0

我有一个文件,其中包含许多逐行的属性文件,可能大约 1000 个,每个属性文件将有大约 5000 个键值对。例如:- 示例示例(abc.txt)-

abc1.properties
abc2.properties
abc3.properties
abc4.properties
abc5.properties

所以我打开这个文件,当它读取每一行时,我在 loadProperties 方法中加载属性文件。并将该属性的键值对存储在 LinkedHashMap 中。

public class Project {
    public static HashMap<String, String> hashMap;

    public static void main(String[] args) {
        BufferedReader br = null;
        hashMap = new LinkedHashMap<String, String>();
        try {
            br = new BufferedReader(new FileReader("C:\\apps\\apache\\tomcat7\\webapps\\examples\\WEB-INF\\classes\\abc.txt"));
            String line = null;
            while ((line = br.readLine()) != null) {
                loadProperties(line);//loads abc1.properties first time
            }
        } catch (FileNotFoundException e1) {
            e1.printStackTrace();
        }
        catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                br.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
//I am loading each property file in this method. And checking whether the key 
already exists in the hashMap if it exists in the hashMap then concatenate the
new key value  with the previous key value. And keep on doing everytime you 
find key exists.

    private static void loadProperties(String line) {
        Properties prop = new Properties();
        InputStream in = Project.class.getResourceAsStream(line);
        String value = null;
        try {
            prop.load(in);
            for(Object str: prop.keySet()) {
                if(hashMap.containsKey(str.toString())) {
                    StringBuilder sb = new StringBuilder().append(hashMap.get(str)).append("-").append(prop.getProperty((String) str));
                    hashMap.put(str.toString(), sb.toString());
                } else {
                    value = prop.getProperty((String) str);
                    hashMap.put(str.toString(), value);
                    System.out.println(str+" - "+value);
                }
            }
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } finally {
            try {
                in.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }


    }

}

所以我的问题是我有超过 1000 个属性文件,每个属性文件都有超过 5000 个键值对。并且大多数属性文件具有相同的键但具有不同的值,因此如果键相同,我必须将值与前一个值连接起来。那么随着属性文件的不断增加以及属性文件中的键值对,LinkedHashMap 的大小是否有任何限制。那么这段代码已经优化到足以处理这类问题了吗?

4

1 回答 1

1

Map 除了为 JVM 分配的内存堆大小之外没有任何限制,并且可以使用选项进行控制-Xmx

从性能的角度来看,您的代码还可以。

但我可以建议以下改进。

  1. 避免使用hashMap.containsKey(str.toString())然后hashMap.get(str)containsKey(key)实现为return get(key) != null,因此您实际上调用get()了两次。您可以改为说以下内容:

    值 = map.get(key); if (value != null) { value += str; } map.put(键,值);

  2. 不要打电话str.toString()。此调用只是创建另一个与原始实例相等的 String 实例。由于 Properties 类未参数化,因此请使用强制转换,即(String)str.

  3. 如果您仍然有性能问题,您可以先合并所有属性文件,然后将它们加载为使用Properties.load()一次。可能您会获得一些性能优势。

于 2011-11-21T07:39:04.777 回答