I am trying to learn java - stream. I am able to do simple iteration / filter / map / collection etc.
When I was kind of trying to collect every 3 elements and print
I solved it like this:
List list = Arrays.asList("a","b","c","d","e","f","g","h","i","j");
int groupBy = 3;
AtomicInteger index = new AtomicInteger(0);
Map> groups = list.stream()
.collect(Collectors.groupingBy(cdm -> index.getAndIncrement()/groupBy));
System.out.println(groups);
It prepares a map where the line number is the key and the strings on the line are in the key.