简体   繁体   English

使用Java 8流将2个不同对象的列表连接到第三个对象的列表

[英]Join list of 2 different object to a list of a third object using Java 8 streams

I came a cross a problem I didn;t manage to solve... 我遇到了一个问题,我没有解决问题......

I have 3 types of objects 我有3种类型的对象

  1. MyObject1 - with fields id and name MyObject1 - 包含字段ID和名称
  2. MyObject2 - with fields id and error MyObject2 - 包含字段ID和错误
  3. MyJoinObject - with fields id, name and error MyJoinObject - 包含字段ID,名称和错误

I have 2 lists: 我有2个清单:

  1. List of MyObject1 MyObject1列表
  2. List of MyObject2 MyObject2列表

I want to create a third list of MyJoinObject, which is a join of the 2 lists. 我想创建MyJoinObject的第三个列表,它是2个列表的连接。 There will be MyJoinObject objects as MyObject1 objects, but they will also contain the error if exists (joined by id). 将MyJoinObject对象作为MyObject1对象,但如果存在,它们也将包含错误(由id加入)。 I want to do that with Java 8 streams . 我想用Java 8流做到这一点。

You can do something like that : 你可以这样做:

List<MyJoinObject> result = 
    list1.stream().map( o1 -> {
        Optional<MyObject2> error = list2.steam()
                                         .filter( o2 -> o2.getId() == o1.getId() )
                                         .findAny();
        if ( error.isPresent() ) 
            return new MyJoinObject( o1.getId(), o1.getName(), error.get().getError() );      

        return new MyJoinObject( o1.getId(), o1.getName() );

    } ).collect( Collectors.toList() );

You can also construct a hasmap of errors mapped by id before doing that by doing : 在执行此操作之前,您还可以构造由id映射的错误的hasmap:

final Map<Integer, MyObject2> errorsById = 
     list2.stream( )
          .collect( Collectors.toMap( MyObject2::getId, Function.identity( ) ) );

If you do that, you can use this map by calling methods containsKey( ) or get( ) to retreive the error 如果这样做,您可以通过调用方法containsKey( )get( )来检索错误来使用此映射

Something like this could work (although I didn't verify): 这样的东西可以工作(虽然我没有验证):

public static void main(String[] args) {
    List<MyObject1> object1list = new ArrayList<>(); // fill with data
    List<MyObject2> object2list = new ArrayList<>();// fill with data

    List<MyJoinObject> joinobjectlist = new ArrayList<>();

    object1list.stream().forEach(
            o1 -> object2list.stream().filter(
                    o2-> o1.getId()==o2.getId()
                    ).forEach(o2->joinobjectlist.add(
                            new JoinObject(o2.getId(), o1.getName(), o2.getError()))
                            )
            );

}

For your information: 供你参考:

public static void main(String[] args) {

    List<MyObject1> myObject1s = new ArrayList<>();
    List<MyObject2> myObject2s = new ArrayList<>();

    // convert myObject2s to a map, it's convenient for the stream
    Map<Integer, MyObject2> map = myObject2s.stream().collect(Collectors.toMap(MyObject2::getId, Function.identity()));

    List<MyJoinObject> myJoinObjects = myObject1s.stream()
                                                 .map(myObject1 -> new MyJoinObject(myObject1, map.get(myObject1.getId()).getError()))
                                                 .collect(Collectors.toList());

}

Of course, there should be a new construction for MyJoinObject, like this: 当然,MyJoinObject应该有一个新结构,如下所示:

public MyJoinObject(MyObject1 myObject1, String error){
    this.id = myObject1.getId();
    this.name = myObject1.getName();
    this.error = error;
}

That's all. 就这样。 :P :P

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM