0

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?

Stefan Zobel
  • 3,182
  • 7
  • 28
  • 38
anna
  • 745
  • 2
  • 9
  • 30
  • 2
    I might be misunderstanding something, but the question at the link is about iteration over a collection (list even), which would have a different solution from iterating over a map with index (I failed to find a neat duplicate). – ernest_k Nov 21 '18 at 18:19
  • I have already read this link. In my example, I would prefer to use the IntStream.range to iterate over the map(doing this I will have the index). But in this case I can't find an elegant way to get the key. – anna Nov 21 '18 at 18:49

0 Answers0