【问题标题】:combine two list of pojos into another list of pojo on the basis of specific value using java 8使用java 8根据特定值将两个pojo列表组合成另一个pojo列表
【发布时间】:2020-07-13 14:01:52
【问题描述】:

我正在尝试使用 java 8 增强我当前的代码

我有两个 pojos 列表

    class A{
    long id;
    String name;
    Sting age;
// getters setters
    }

    class B{
    long id;
    String address;
    String city;
// getters setters
    }

必修课:

    class C{
    long id;
    String name;
    Sting age;
    String address;
    String city;
// getters setters
    }

我有list<A>List<B>,并且需要最终的List<C>。我已经使用 for 循环和所有内容完成了这项工作。但我现在想简化使用 java 8 功能。

非常感谢任何帮助。

编辑

 List<C> cList=new ArrayList<>();
    C obj=new C();
    for(A aa: aList){
    C obj=new C();
     obj.setId(a.getId);
     obj.setName(a.getName);
     obj.setAge(a.getAge);
       for(B bb: bList){
         if(aa.getId==bb.getId(){
           obj.setAddress(bb.setAddress);
           obj.setCity(bb.setCity);
          }
       } 
       cList.add(obj); 
    }

【问题讨论】:

  • 您能否分享您的循环,以便我们帮助您将其变成适用于 Java 8 的东西?
  • @ShanS 我已经为这个案例添加了我现有的代码

标签: java list arraylist java-8 pojo


【解决方案1】:

如果您只想将代码传输到流中,您可以:

List<C> collect = aList.stream()
        .map(a -> {
            C obj = new C();
            obj.setId(a.getId());
            obj.setName(a.getName());
            obj.setAge(a.getAge());
            bList.stream()
                    .filter(b -> a.getId() == b.getId())
                    .findFirst()
                    .ifPresent(b -> {
                        obj.setAddress(b.getAddress());
                        obj.setCity(b.getCity());
                    });
            return obj;
        }).collect(toList());

但最好为bList引入map,并将其用作查找后者。

Map<Long, B> map = bList.stream()
        .collect(Collectors.toMap(B::getId, b -> b));

List<C> collect = aList.stream()
        .map(a -> {
            C obj = new C();
            obj.setId(a.getId());
            obj.setName(a.getName());
            obj.setAge(a.getAge());
            if (map.containsKey(a.getId())) {
                B b = map.get(a.getId());
                obj.setAddress(b.getAddress());
                obj.setCity(b.getCity());
            }
            return obj;
        }).collect(toList());

【讨论】:

    【解决方案2】:

    我能想到的最简洁的方法是使用HashMap 而不是List,然后使用Id 作为键,将实际对象作为值,这样可以更轻松地查找。同时为C生成一个完整的构造函数:

    A a1 = new A(1234, "foo", "bar");
    A a2 = new A(12347, "baz", "quux");
    B b1 = new B(2345, "example street", "city");
    B b2 = new B(1234, "test street", "town");
    
    HashMap<Long, A> aHashMap = new HashMap<Long, A>();
    HashMap<Long, B> bHashMap = new HashMap<Long, B>();
    
    aHashMap.put(a1.getId(), a1);
    aHashMap.put(a2.getId(), a2);
    
    bHashMap.put(b1.getId(), b1);
    bHashMap.put(b2.getId(), b2);
    
    List<C> cList = aHashMap.entrySet().stream()
            .filter(entry->bHashMap.containsKey(entry.getKey()))
            .map(entry -> {
                B matchingB = bHashMap.get(entry.getKey());
                return new C(entry.getKey(),
                        entry.getValue().getName(),
                        entry.getValue().getAge(),
                        matchingB.getAddress(),
                        matchingB.getCity());
            })
            .collect(Collectors.toList());
    
    System.out.println(cList.toString());
    

    返回(我换了toString()):

    [C{id=1234, name='foo', age='bar', address='test street', city='town'}]
    

    【讨论】:

    • 感谢您的回复,在提到的解决方案中,当我从 jdbcTemplate.query(String,rowMapper) 获取 list 时,我必须添加额外的代码才能将列表转换为 hashmap,但可以肯定我可以试一试
    • 您可以使用 lczapski 的答案中提到的Map&lt;Long, B&gt; map = bList.stream().collect(Collectors.toMap(B::getId, b -&gt; b)); sn-p 来获取地图。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-12-05
    • 2017-02-09
    • 1970-01-01
    • 2023-01-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多