I'm trying to build a "HashMap< String,ArrayList< String>>" with the same ArrayList cleaning it and putting it again with different values.
The problem comes when the HashMap automatically updates all it's content when I update the ArrayList.
I update the HashMap with "put" and the same variable with different value as a key.
Is there any way to load the data?
The code is the next:
` StringBuilder sb = new StringBuilder();
try (BufferedReader br = Files.newBufferedReader(Paths.get("testFile"))) {
// read line by line
String line;
Map<String,ArrayList<String>> trigg = new HashMap<String,ArrayList<String>>();
Map<String,Map<String,ArrayList<String>>> charac = new HashMap<String,Map<String,ArrayList<String>>>();
ArrayList<String> cond = new ArrayList<String>();
String triggName = "";
String condName = "";
while ((line = br.readLine()) != null) {
sb.append(line).append("\n");
if (line.length() > 3) {
if (line.charAt(0) == (char) 9 && line.charAt(1) == (char) 9 && line.charAt(2) == (char) 9 && line.charAt(3) == (char) 9) {
cond.add(line);
}else if (line.charAt(0) == (char) 9 && line.charAt(1) == (char) 9 && line.charAt(2) == (char) 9) {
if (condName != "" && cond.get(0) != null) {
trigg.put(condName+"", new ArrayList<String>() {{
for (String st : cond) {
add(st);
}
}});
}
cond.clear();
condName = line.split("\\[")[1].split("]")[0];
} else if (line.charAt(0) == (char) 9 && line.charAt(1) == (char) 9) {
System.out.println(cond + condName);
if (condName.equals("C2")) {
System.out.println(trigg);
}
if (condName != "" && cond.get(0) != null) {
trigg.put(condName, new ArrayList<String>() {{
for (String st : cond) {
add(st);
}
}});
for(Map.Entry<String,ArrayList<String>> entry : trigg.entrySet()) {
String key = entry.getKey();
ArrayList<String> value = entry.getValue();
charac.put(triggName, Map.of(key,value));
}
}
cond.clear();
condName = "";
triggName = line.split("\\[")[1].split("]")[0];
} else if (line.charAt(0) == (char) 9) {
//System.out.println("Un solo tab " + line);
} else {
//System.out.println("No hay tabs " + line);
}
}
}
trigg.put(condName, new ArrayList<String>() {{
for (String st : cond) {
add(st);
}
}});;
charac.put(triggName,trigg);;
for(Map.Entry<String,ArrayList<String>> entry : trigg.entrySet()) {
String key = entry.getKey();
ArrayList<String> value = entry.getValue();
charac.put(triggName, Map.of(key,value));
}
System.out.println(charac);
} catch (IOException e) {
System.err.format("IOException: %s%n", e);
}`
The file I'm reading is:
[SPEECH]
[character]
[loc1]
[C1]
=test1
[loc2]
[C1]
test2
=test3
[C2]
test4
[loc3]
[C1]
test5
=test6
[C2]
test7
=test8
=test9
This should build a character HashMap with all this content sorted with the same hierarchy, but when I load more than one C* ArrayList for the same loc, the last C* that existed in the same loc is removed.
The output I'm getting is:
{loc2={C2=[ test4]}, loc3={C2=[ test7, =test8, =test9]}, loc1={C1=[ =test1]}}
I don't know where I'm pissing it.