0

I have a function which returns Map of String to List i.e Map<String,List<Integer>> here is my piece of code

   Map<String,List<Integer>> myList=new TreeMap<String, List<Integer>>();
      List<Integer> firstList=new ArrayList<Integer>();
      firstList.add(1);
      firstList.add(2);
      firstList.add(3);
      myList.put("1", firstList);
      List<Integer> secodList=new ArrayList<Integer>();
      secodList.add(3);
      secodList.add(4);
      secodList.add(5);
      myList.put("2", secodList);
      for (int i = 0; i < myList.get("1").size(); i++) {
         for (int j = 0; j < myList.get("2").size(); j++) {
            System.out.println(myList.get("1").get(i) +"  "+myList.get("2").get(j));
          }
        }

For the time being i have iterated over the loop since i know there is only 2 entries . But myListMap is growable it might have 3 entries , or even 4 entries how do i dynamically construct the for loop .

Unknown Guy
  • 107
  • 3
  • 3
  • 11
  • Hint: `myList.entrySet()` returns `Set>`. See https://docs.oracle.com/javase/8/docs/api/java/util/Map.html#entrySet--. Also see: https://stackoverflow.com/questions/46898/how-do-i-efficiently-iterate-over-each-entry-in-a-java-map – Daniel Cheung Sep 22 '21 at 17:48

1 Answers1

-1

To iterate a Map <key, value> you can use myMap.entrySet (). This method:

Returns a Set view of the mappings contained in this map

more information in the documentation suggested in Daniel Cheung comment

I give two versions of the same solution, one simplified and the other making each step explicit.

lists and map on which to iterate

 /**
 * int lists are created
 **/
List<Integer> firstList=new ArrayList<Integer>();
firstList.add(1);
firstList.add(2);
firstList.add(3);

List<Integer> secodList=new ArrayList<Integer>();
secodList.add(5);
secodList.add(6);
secodList.add(7);

List<Integer> tList=new ArrayList<Integer>();
tList.add(8);
tList.add(9);
tList.add(10);


/**
 * the map is created, the lists of int and keys are added
 **/
Map<String, List<Integer>> myMap=new TreeMap<String, List<Integer>>();

myMap.put("one", firstList);
myMap.put("two", secodList);
myMap.put("three", tList);

1 Explained version

//a set of inputs of type <String, List <Integer>> is created
Set<Map.Entry<String,List<Integer>>> mapAsSet;
//each entry in myMap is assigned
mapAsSet= myMap.entrySet();

//iterates through each entry in the set
for (Map.Entry<String, List<Integer>> entry : mapAsSet) {
    
    System.out.println(entry);
}

Output

one=[1, 2, 3]
three=[8, 9, 10]
two=[5, 6, 7]

2 Simplified version

//The "entry" set is created and the for iterates through each key and its associated value.
for (Map.Entry<String, List<Integer>> entry : myMap.entrySet()) {

    //with entry.getValue () we access its value, which is the list of int corresponding to each "entry" of the set
    //with .foreach (v-> action) you access each value of the int list corresponding to each "entry" of the for.
    entry.getValue().forEach(j-> System.out.println(entry.getKey() +"  "+ j) );
}

Output

one  1
one  2
one  3
three  8
three  9
three  10
two  5
two  6
two  7
Federremu
  • 84
  • 1
  • 7
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Sep 22 '21 at 19:32
  • @Federremu it will not work for more than 2 entries – Unknown Guy Sep 23 '21 at 02:10
  • I edit the answer to make it clearer. The code was tested and works on any number of map elements. Let me know if I misinterpreted your question @Unknown Guy. – Federremu Sep 23 '21 at 14:37