I am new to Java 8. I am learning the reduce API thread. I see strange behavior with this code:
public class PrdefinedCollectors { public static void main(String[] args) { Stream<Integer> stream = Stream.of(1, 2, 3, 4, 5, 6); List<Integer> dataHolder = new ArrayList<Integer>(); List<Integer> numbers = stream.reduce(dataHolder, (List<Integer> dataStore, Integer data) -> { System.out.println(data + " ->: " + dataStore); dataStore.add(data); return dataStore; }, (List<Integer> listOne, List<Integer> listTwo) -> { System.out.println("ListOne Data :" + listOne + " List Two data :" + listTwo); listOne.addAll(listTwo); return listOne; }); System.out.println(numbers); } }
Output:
1 ->: [] 2 ->: [1] 3 ->: [1, 2] 4 ->: [1, 2, 3] 5 ->: [1, 2, 3, 4] 6 ->: [1, 2, 3, 4, 5] [1, 2, 3, 4, 5, 6]
My question is why the combiner function does not fulfill the value, why this line:
System.out.println("List One Data: " + listOne + " List Two data: " + listTwo);
... not running?
java java-8 java-stream
vicky
source share