首页 > 解决方案 > 使用 java 8 流在 2 个列表中查找元素匹配并从另一个列表值更新一个列表值

问题描述

我有两个对象列表。需要遍历它们,通过 id 匹配它们,然后如果 id 匹配,则从列表 2 更新该对象的 list1 值。

我知道如何用旧的 java 来做,但想知道它是否可以在 JAVA8 (stream) 中完成

class Person {
    String id ;
    String name;
    String age;
}
p1:{1,test1,3}
p2:{2,test2,6}
p3:{3,test3,8}
p4:{1,test1,33}
p5:{2,test22,16}
p6:{3,test3,18}
p10:{10,test10,8}

List<Person> list1 = {p1,p2, p3, p10};
List<Person> list2 = {p4,p5, p6}; 

想要这样的最终结果

list1 = {p1,p2, p3, p10}
p1:{1,test1,33}
p2:{2,test22,16}
p3:{3,test3,18}
p10:{10,test10,8}

基本上,如果 id 匹配,我希望将值从 list2 覆盖到 list 1(不想要 list 3)

到目前为止,我能够通过 id 匹配它们,但后来我迷失了如何更新字段......

List<CartItemAttribute> test = existingCartItem.get().getAttributes().stream()
                        .filter(x -> (cartItem.getAttributes().stream()
                        .filter(y->y.getCartItemAttributeId()== x.getCartItemAttributeId())
                        .count()<1
)).collect(Collectors.toList());

这是我所拥有的按预期工作

private void upsertAttributes(CartItem cartItem, Optional<CartItem> existingCartItem) {
        boolean addAtribute = true;
        List<CartItemAttribute> existingAttribute = existingCartItem.get().getAttributes();
        for (CartItemAttribute requestingList: cartItem.getAttributes()) {
            addAtribute = true;
            for (CartItemAttribute existingList: existingAttribute) {
                if(StringUtils.isNotBlank(requestingList.getCartItemAttributeId()) 
                              && requestingList.getCartItemAttributeId()
                                .equals(existingList.getCartItemAttributeId())) {

                    existingList.setKey(requestingList.getKey());
                    existingList.setValue(requestingList.getValue());
                    addAtribute = false;
                }
            }
            if(addAtribute) {
                existingAttribute.add(requestingList);
            }   
        }
}

标签: javajava-8java-stream

解决方案


试试这个

List<Person> list1 = getPersonList1();
List<Person> list2 = getPersonList2();

list1.replaceAll(l1 -> list2.stream()
          .filter(l2 -> l2.getId().equals(l1.getId()))
          .findAny()
          .orElse(l1));

list1.addAll(list2.stream()
          .filter(l2 -> !list1.contains(l2))
          .collect(toList()));

获取人员列表1

private static List<Person> getPersonList1() {
    return Lists.newArrayList(
        new Person("1", "test1", "3"),
        new Person("2", "test2", "6"),
        new Person("3", "test3", "8"),
        new Person("10", "test10", "8")
    );
}

获取人员列表2

private static List<Person> getPersonList2() {
    return Lists.newArrayList(
        new Person("1", "test1", "33"),
        new Person("2", "test22", "16"),
        new Person("3", "test3", "18"),
        new Person("4", "test4", "15")
    );
}

输出

清单 1

[
Person(id=1, name=test1, age=3), 
Person(id=2, name=test2, age=6), 
Person(id=3, name=test3, age=8), 
Person(id=10, name=test10, age=8)
]

清单2

[
Person(id=1, name=test1, age=33), 
Person(id=2, name=test22, age=16), 
Person(id=3, name=test3, age=18), 
Person(id=4, name=test4, age=15)
]

最终名单1

[
Person(id=1, name=test1, age=33), 
Person(id=2, name=test22, age=16), 
Person(id=3, name=test3, age=18), 
Person(id=10, name=test10, age=8), 
Person(id=4, name=test4, age=15)
]

推荐阅读