List<Integer> integers = Arrays.asList(1, 2, 3, 5, 6, 8, 9, 10);
integers.stream().filter((integer) -> integer % 2 == 0).collect(Collectors.toList());
As shown above integers
is a List, from which we need to filter only even numbers. I can achieve by using .filter()
method. But, is there any possibility to achieve the same with .reduce()
method. Hope, .reduce()
method filtered out all the other elements by performing given BynaryOperation and return reduced list.
If my understanding is not correct on .reduce()
method, please let me know what exactly this method do.
Your understanding of reduction is wrong. reduce
will apply a function on all elements repeatedly to get one single result.
You seem to think of reduce like doing
1, 2, 3, 5, 6, 8, 9, 10
│ │ │ │ │ │ │ │
└op┘ └op┘ └op┘ └op┘
│ │ │ │
result list
whereas, in fact, it does
1, 2, 3, 5, 6, 8, 9, 10
│ │ │ │ │ │ │ │
└op┘ └op┘ └op┘ └op┘
│ │ │ │
└─op─┘ └─op─┘
│ │
└────op────┘
│
final result value
Though, this is a conceptional view, the exact order of operations is unspecified. A sequential execution will be like (((1 op 2) op 3) op 4)…
while a parallel execution will be a mixture of an execution like the tree above and partial sequential execution(s).
You can abuse reduce
to create a result list if you first convert each element into a List
and then use a list operation which concatenates each list, however, there are two problems with this:
op
function which does that in every possible execution scenarioThe latter point can be solved by using collect
, which is a mutable reduction, hence, allows you to use mutable lists to which you can add items, however, it does not address the first point, including the desired filter would violate the contract and only work in a sequential execution.
So the solution is to define a filter
for all elements in the scope of the source list, followed by a mutable reduction to create the result list using collect
, and, big surprise, that’s exactly what you original code does:
… .filter(integer -> integer % 2 == 0).collect(Collectors.toList());
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With