I am learning how to use Java streams, and you need some help to figure out how to pass nested collections and collect the results back to the collection.
In the simple example below, I created 2 ArrayLists and added them to the ArrayList. I want to be able to perform a simple function for each nested collection, and then write the results to a new collection. The last line of code does not even compile. Any explanation would be appreciated!
ArrayList<Integer> list1 = new ArrayList<Integer>(Arrays.asList(1,2,3)); ArrayList<Integer> list2 = new ArrayList<Integer>(Arrays.asList(4,5,6)); ArrayList<ArrayList<Integer>> nested = new ArrayList<ArrayList<Integer>>(); nested.add(list1); nested.add(list2); ArrayList<ArrayList<Integer>> result = nested.stream() .map(list -> list.add(100)) .collect(Collectors.toList());
source share