Java 8 – filtering with bipredicate
•
Java
I have a stream of integers. I want to find two numbers whose sum is equal to the other number So I came up with the following solutions:
BiPredicate<Integer,Integer> p = (price1,price2) -> price1.intValue() + price2.intValue() == moneyQty; flavoursPrices.filter(p);
However, the filtering method did not receive bipredicate Why not? What are the alternatives?
Solution
Where can you implement the solution through streamex Library:
StreamEx.ofPairs(prices,(p1,p2) -> p1 + p2 == 5 ? StreamEx.of(p1,p2) : StreamEx.empty()) .flatMap(Function.identity()) .forEach(price -> System.out.println(price));
You may also want to create your own classes to encapsulate pairs
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码