0

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.

Roberto
  • 1
  • 1
  • Fixed!!! I have replaced the code for putting things into charac for `charac.put(triggName, new HashMap>()); charac.get(triggName).putAll(trigg);`. Now everything works fine. Thanks all for helping. – Roberto Feb 02 '20 at 17:29

1 Answers1

0

I'm trying to build a HashMap<String,ArrayList<String>> with the same ArrayList cleaning it and putting it again with different values.

That's the wrong way, since all the keys of your HashMap are mapped to the same ArrayList object.

Instead of trying to re-use the same ArrayList object, you should create a new ArrayList each time you put a new entry in the HashMap.

Eran
  • 387,369
  • 54
  • 702
  • 768
  • I fixed that problem, but now, my problem is the key. Now I'm generating a new ArrayList object, but don't know how could I get the value of my String variable instead of using the same variable as a key. – Roberto Feb 02 '20 at 11:27
  • @Roberto what's your problem with the key? Please edit your question with relevant code. Otherwise it's hard to answer. – Eran Feb 02 '20 at 11:28
  • I build the array, and I do HashMap.put(miStringVariable, new ArrayList(String){ add("whatever") }). Let me try to add an example of what happens. – Roberto Feb 02 '20 at 11:29
  • Thanks @RealSkeptic, will try to fix all things you said and try it again. Really appreciate the help!! – Roberto Feb 02 '20 at 13:49
  • Still unable to make it, cause with compute will edit current entries. My problem is now that when I put a C2 array into a loc map, the C1 that existed before is replaced by the new one. – Roberto Feb 02 '20 at 16:13