简体   繁体   English

如何使用 Lambda 表达式将一组值从 ArrayList 添加到另一个 ArrayList

[英]How to add a set of values from an ArrayList to another ArrayList using Lambda expression

I have to create a result list by adding objects from one array list to another.我必须通过将对象从一个数组列表添加到另一个来创建一个结果列表。 Here is my code that works.这是我的工作代码。

    private List<MyObject> getObjectList(List<OtherObject> objects) {
        List<MyObject> resultList = new ArrayList<>();
        for (int i = 0; i < objects.size(); i++) {
        MyObject object = new MyObject()
            object.setId(objects.get(i).getId());
            object.setText(objects.get(i).getTextValue());
            object.setUserId(objects.get(i).getUserName());
            object.setCreatedDate(objects.get(i).getCreateDateTime());

            resultList.add(object);
        }
        
        return resultList;
    }

How can I achieve this by using lambda expression?如何通过使用 lambda 表达式来实现这一点?

Here's how you can do it with a method reference:以下是使用方法引用的方法:

private List<MyObject> getObjectList(List<OtherObject> objects) {
    return objects.stream()
        .map(this::map)
        .collect(Collectors.toList());
}

private MyObject map(OtherObject otherObject) {
    MyObject object = new MyObject();
    object.setId(otherObject.getId());
    object.setText(otherObject.getTextValue());
    object.setUserId(otherObject.getUserName());
    object.setCreatedDate(otherObject.getCreateDateTime());
    return object;
}

Here's how you can do it using streams and lambda expression:以下是使用流和 lambda 表达式的方法:

private List<MyObject> getObjectList(List<OtherObject> objects) {
    return objects.stream()
        .map(obj -> {
        MyObject object = new MyObject();
        object.setId(obj.getId());
        object.setText(obj.getTextValue());
        object.setUserId(obj.getUserName());
        object.setCreatedDate(obj.getCreateDateTime());
        return object;
    }).collect(Collectors.toList());
}

Here is an example:下面是一个例子:

    private List<MyObject> getObjectList(List<OtherObject> objects) {
        List<MyObject> resultList = new ArrayList<>();
        objects.forEach(obj -> {
            MyObject object = new MyObject();
            object.setId(obj.getId());
            object.setText(obj.getTextValue());
            object.setUserId(obj.getUserName());
            object.setCreatedDate(obj.getCreateDateTime());
            resultList.add(object);
        });
        return resultList;
    }

Using Record使用记录

Equivalent of your OtherObject相当于您的OtherObject

import java.time.LocalDateTime;

public record OtherRec(int            id,
                       String         textValue,
                       String         userName,
                       LocalDateTime  createDateTime) {
}

Equivalent of your MyObject相当于你的MyObject

import java.time.LocalDateTime;

public record MyRecord(int id,
                       String text,
                       String userId,
                       LocalDateTime createDate) {
}

Copying list of OtherRec to MyRecordOtherRec列表复制到MyRecord

import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

public class CopyList {

    public static void main(String[] args) {
        List<OtherRec> source = List.of(new OtherRec(1, "First", "One", LocalDateTime.of(2020, 10, 5, 12, 0)),
                                        new OtherRec(2, "Second", "Two", LocalDateTime.of(2020, 10, 5, 13, 0)));
        List<MyRecord> target = source.stream()
                                      .map(or -> new MyRecord(or.id(), or.textValue(), or.userName(), or.createDateTime()))
                                      .collect(Collectors.toList());
        System.out.println(target);
    }
}

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

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