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

huangapple go评论73阅读模式
英文:

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

问题

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

    return resultList;
}
英文:

I have to create a result list by adding objects from one array list to another. Here is my code that works.

    private List&lt;MyObject&gt; getObjectList(List&lt;OtherObject&gt; objects) {
        List&lt;MyObject&gt; resultList = new ArrayList&lt;&gt;();
        for (int i = 0; i &lt; 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?

答案1

得分: 3

这是使用方法引用的方法:

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;
}

这是使用流和 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's how you can do it with a method reference:

private List&lt;MyObject&gt; getObjectList(List&lt;OtherObject&gt; 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:

private List&lt;MyObject&gt; getObjectList(List&lt;OtherObject&gt; objects) {
    return objects.stream()
        .map(obj -&gt; {
        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());
}

答案2

得分: 1

以下是一个示例:

    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;
    }
英文:

Here is an example:

    private List&lt;MyObject&gt; getObjectList(List&lt;OtherObject&gt; objects) {
        List&lt;MyObject&gt; resultList = new ArrayList&lt;&gt;();
        objects.forEach(obj -&gt; {
            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;
    }

答案3

得分: 0

使用 [Record][1]

与你的 `OtherObject` 等效
```java
import java.time.LocalDateTime;

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

与你的 MyObject 等效

import java.time.LocalDateTime;

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

OtherRec 列表复制到 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);
    }
}

<details>
<summary>英文:</summary>

Using [Record][1]

Equivalent of your `OtherObject`
```java
import java.time.LocalDateTime;

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

Equivalent of your MyObject

import java.time.LocalDateTime;

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

Copying list of OtherRec to MyRecord

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

public class CopyList {

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

huangapple
  • 本文由 发表于 2020年10月5日 23:34:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/64211771.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定