简体   繁体   English

Lambda表达式将对象从一个列表添加到另一个列表类型

[英]Lambda expression to add objects from one list to another type of list

There is a List<MyObject> and it's objects are required to create object that will be added to another List with different elements : List<OtherObject> . 有一个List<MyObject> ,它的对象是创建将添加到具有不同元素的另一个List的对象所必需的: List<OtherObject>

This is how I am doing, 这就是我的表现,

List<MyObject> myList = returnsList();
List<OtherObj> emptyList = new ArrayList();

for(MyObject obj: myList) {   
    OtherObj oo = new OtherObj();
    oo.setUserName(obj.getName());
    oo.setUserAge(obj.getMaxAge());   
    emptyList.add(oo);  
}

I'm looking for a lamdba expression to do the exact same thing. 我正在寻找一个lamdba表达式来完成同样的事情。

If you define constructor OtherObj(String name, Integer maxAge) you can do it this java8 style: 如果定义构造函数OtherObj(String name, Integer maxAge) ,则可以使用java8样式:

myList.stream()
    .map(obj -> new OtherObj(obj.getName(), obj.getMaxAge()))
    .collect(Collectors.toList());

This will map all objects in list myList to OtherObj and collect it to new List containing these objects. 这将在列表中的所有对象映射myListOtherObj并收集到新的List包含这些对象。

You can create a constructor in OtherObject which uses MyObject attributes, 您可以在OtherObject中使用MyObject属性创建构造函数,

public OtherObject(MyObject myObj) {
   this.username = myObj.getName();
   this.userAge = myObj.getAge();
}

and you can do following to create OtherObject s from MyObject s, 并且您可以执行以下操作来从MyObject创建OtherObject

myObjs.stream().map(OtherObject::new).collect(Collectors.toList());

I see that this is quite old post. 我看到这是相当古老的帖子。 However, this is my take on this based on the previous answers. 但是,这是基于之前的答案我对此的看法。 The only modification in my answer is usage of .collect(ArrayList::new, ArrayList::add,ArrayList:addAll) . 我的答案中唯一的修改是使用.collect(ArrayList::new, ArrayList::add,ArrayList:addAll)

Sample code : 示例代码:

List<OtherObj> emptyList = myList.stream()
.map(obj -> {   
OtherObj oo = new OtherObj();
oo.setUserName(obj.getName());
oo.setUserAge(obj.getMaxAge());   
return oo; })
.collect(ArrayList::new, ArrayList::add,ArrayList::addAll);

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

相关问题 如何使用lambda表达式基于一个变量对对象列表进行排序? - How to sort a List of objects based on one variable using lambda expression? 在 Java 中使用 lambda 对来自另一个类的对象列表进行排序 - Sorting list of objects from another Class with lambda in Java 如何使用Java 8 Lambda表达式将一种类型的List转换为其子类型的List - How to use a Java 8 Lambda expression to convert a List of one type to a List of its subtype 如何使用lameda Expression Java将列表从一种类型转换为另一种类型 - How convert list from one to another type using lameda Expression java 将一种类型的列表添加到另一种类型的列表 - adding a list of one type to a list of another type 将项目从一个列表添加到Java中的另一个列表 - Add the items from one list to another list in java Jlist中的问题从一个列表添加到另一列表 - Problem in Jlist add from one list to another list 列表中的Lambda表达式不起作用 - Lambda expression in a List not working Java-Streams - filter() + map() - Map 来自一个列表的对象缺少属性到另一个列表的对象 - Java-Streams - filter() + map() - Map objects from one List with the missing property to objects from another List 如何在Java中将数据从一个对象列表复制到另一个对象? - How to copy data from one list of objects to another in java?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM