Iterate Map using forEach and keep track of index

99 Views Asked by At

Let's say we have a EnumMap in java and through the iteration using streams we need to use the key of the map, the value and the index. I have implemented as in the following example:

EnumMap<TestEnum, List<String>> enumMap = new EnumMap<TestEnum, List<String>>(TestEnum.class);
enumMap.put(TestEnum.ONE, Arrays.asList("one", "1"));
enumMap.put(TestEnum.TWO, Arrays.asList("two", "2"));

final int[] position= {0};
enumMap.entrySet().forEach( e -> {
    List<String> objects= enumMap.get(e.getKey());
    System.out.println("Position: " position[0] + 1);
    position[0]++;
});

What is the best way to implement this?

0

There are 0 best solutions below