从单个文本文件加载大量属性文件并插入LinkedHashMap

时间:2011-11-21 07:19:30

标签: java properties hashmap linkedhashmap

我有一个文件,其中包含许多属性文件,每行可能大约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的大小也有限制。所以这段代码已经足够优化以处理这类问题了吗?

1 个答案:

答案 0 :(得分:1)

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

进行控制

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

但我可以建议以下改进。

  1. 避免使用hashMap.containsKey(str.toString()),然后使用hashMap.get(str)containsKey(key)已实施为return get(key) != null,因此您实际上会拨打get()两次。你可以这样说:

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

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

  3. 如果仍然存在性能问题,可以先合并所有属性文件,然后使用Properties.load()加载一次。可能会有一些性能上的好处。