Java 8 Lambda - Intersection of Two Lists

生来就可爱ヽ(ⅴ<●) 提交于 2019-11-26 19:57:03

问题


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.


回答1:


The simplest approach is this:

List<T> intersect = list1.stream()
                         .filter(list2::contains)
                         .collect(Collectors.toList());



回答2:


I need to compare them on assume list1.id == list2.fk_id

First build up a set of the fk_id;

Set<Integer> orderLineEntrSet = orderEntry.getOrderReleases().stream()
    .flatMap(orderReleaseEntry ->
orderReleaseEntry.getOrderLines().stream())
    .filter(orderLineEntry -> { 
            String s = orderLineEntry.getStatus(); 
            return "PP".equals(s) || "PD".equals(s); 
    })
    .map(e -> e.getId())
    .collect(Collectors.toSet());

double[] totalAmount = { 0.0 };
double[] couponDiscount = { 0.0 };
orderLineEntryList.stream()
    .flatMap(sre -> sre.getLineEntries().stream())
    .filter(ole -> orderLineEntrySet.contains(ole.getOrderLineId())
    .filter(ole -> !"PX".equals(ole.getStatusCode()))
    .forEach(ole -> {
            totalAmount[0] += ole.getFinalAmount();
            if (ole.getCouponDiscount() != null)
                couponDiscount[0] += ole.getCouponDiscount();
        });

You can avoid using a reference to an array object by using reduce function. e.g. see how Collectors.averagingDouble is implemented. But I find this more complicated.

Note: this is O(N) by using a set of ids rather than using a list of matching ids which would be O(N^2)




回答3:


List<T> intersect = list1.stream().filter(set1::contains).collect(Collectors.toList());

This will work as long as T is a String, Integer, Float, etc. where the equals and HashCode methods are straightforward. But if the T is a custom object, we need to implement HashCode & equals



来源:https://stackoverflow.com/questions/31683375/java-8-lambda-intersection-of-two-lists

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!