行ごとに多くのプロパティファイルを含むファイルがあり、約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 のサイズに制限があり、プロパティ ファイルのキーと値のペアも制限されます。このコードは、この種の問題を処理するのに十分最適化されていますか?