Hey.

There is a code:

Map<String, List<String>> HashMap = new HashMap<String, List<String>>(); List<String> string_list = new ArrayList<String>(); string_list.add("Whysss?"); string_list.add("a1_q"); string_list.add("a2_q"); HashMap.put("ONE", string_list); string_list.add("Whysss?_2"); string_list.add("a1_q_2"); string_list.add("a2_q_2"); HashMap.put("TWO", string_list); System.out.println(HashMap.get("ONE")); System.out.println(HashMap.get("TWO")); Результат: [Whysss?, a1_q, a2_q, Whysss?_2, a1_q_2, a2_q_2] [Whysss?, a1_q, a2_q, Whysss?_2, a1_q_2, a2_q_2] 

The question is that both the "ONE" key and the "TWO" key return the same block, and I need the "ONE" to return [Whysss ?, a1_q, a2_q], a "TWO" [Whysss? _2, a1_q_2, a2_q_2].

Can not use HashMap? Is there any more convenient implementation of a multidimensional associative array in JAVA?

    2 answers 2

    You have stored a string, a link in the map. You can check by adding more elements to the string_list variable and re-doing Map.get()

    Here’s an option:

     HashMap.put("ONE", new ArrayList<String>(Arrays.asList("el1", "el2"))); HashMap.put("TWO", new ArrayList<String>(Arrays.asList("el3", "el4"))); 
    • Thank you very much. Exactly what is needed. Briefly and clearly. And indeed, I’m pointing to the link ... something is not right in the morning, I don’t notice the elementary :) - Sever

    And you try to HashMap different elements in HashMap . And even in the first and in the second case, you put in the same list. Of course, then in the first and in the second key (ONE, TWO), it will be the same.

    • Thank. :) In the morning I'm drowning something. - Sever