I am trying to find intersection
of two lists based on some condition and doing some steps. Couldn't find a way to do it (in learning stage) :)
Double totalAmount = 0.00d; Double discount = 0.00d; List<OrderLineEntry> orderLineEntryList = orderEntry.getOrderReleases().stream() .flatMap(orderReleaseEntry -> orderReleaseEntry.getOrderLines().stream()) .filter(orderLineEntry -> orderLineEntry.getStatus().equals("PP") || orderLineEntry.getStatus().equals("PD")) .collect(Collectors.toList()); for (OrderLineEntry orderLineEntry : orderLineEntryList) { for (SplitLineEntry splitLineEntry : splitReleaseEntry.getLineEntries()) { if (splitLineEntry.getOrderLineId().equals(orderLineEntry.getId()) && splitLineEntry.getStatusCode() != "PX") { totalAmount += orderLineEntry.getFinalAmount(); couponDiscount += orderLineEntry.getCouponDiscount() == null ? 0.00d : orderLineEntry.getCouponDiscount(); } } }
As you see, the logic is simple
Get All items from order based on some filter list
and intersect with another list
and do some stuff.
Intersection of Two Lists of StringsList<String> list = Arrays. asList("red", "blue", "blue", "green", "red"); List<String> otherList = Arrays. asList("red", "green", "green", "yellow");
You can transform the lists to sets, and then use Set. retainAll method for intersection between the different sets. Once you intersect all sets, you are left with the common elements, and you can transform the resulting set back to a list.
intersection() returns an unmodifiable view of the intersection of two sets. The returned set contains all elements that are contained by both backing sets. The iteration order of the returned set matches that of set1. Return Value: This method returns an unmodifiable view of the intersection of two sets.
The simplest approach is this:
List<T> intersect = list1.stream() .filter(list2::contains) .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