0

I am trying to iterate over a list of maps in java, but I can not figure it out how. I need to keep the index as well.

for example, I have a list of maps like

List<Map<String, String>> ListOfMaps = new ArrayList<>();

and the output that I want is something like:

"ListOfMaps[index].map(k) => ListOfMaps[index].map(k).getValue()"

I appreciate if you can give me some hint how I can do it using java stream operators, with high performance.

Stefan Zobel
  • 3,182
  • 7
  • 28
  • 38
farshad
  • 189
  • 1
  • 1
  • 6
  • Also check out https://www.baeldung.com/java-stream-indices – JoschJava Oct 08 '19 at 13:45
  • [Program to Iterate over a Stream with Indices in Java 8](https://www.geeksforgeeks.org/program-to-iterate-over-a-stream-with-indices-in-java-8/) – Ole V.V. Oct 08 '19 at 13:45
  • 1
    I find this question very unclear. Can you please provide an example input and the expected output for that input? – marstran Oct 08 '19 at 13:46

1 Answers1

0

As an advocate for streams, I personally don't like using them here. But I will anyhow:

IntStream.range(0, mapList.size())
    .forEach(index -> {
        Map<String, String> map = mapList.get(index);
        System.out.println("ListOfMaps[" + index + "]" ...);
    })
Rogue
  • 11,105
  • 5
  • 45
  • 71