如何将列表中的值映射到<String>列表中<Model>



我正在练习Java 8功能,遇到了一个无法解决的问题:

我有一个字符串列表,我需要将其映射到客户列表串列表ustomer Model有其他属性:

public class Customer {
private int id{get; set;};
private String name{get; set;};
private String company{get; set;};
}
public static List<Customer> convertList() { 
List<Customer> customerList = new ArrayList<Customer>();

List<String> nameList = new ArrayList<String>();
nameList.add("Customer A");
nameList.add("Customer B");
nameList.add("Customer C");
nameList.add("Customer D");
nameList.add("Customer E");
nameList.add("Customer F");

return customerList.stream()
.map()//here I got stuck
.collect(Collectors.toList()); 
}

我想做的是将List的值设置到List的Name属性中,我尝试使用流和映射,但不知道如何在这种情况下使用它。

List<Customer> customerList = nameList.stream().map(name -> {
Customer c = new Customer();
c.setName(name);
c.setId(generateId());
c.setCompany(getCompany(name));
return c;
}).collect(Collectors.toList());

您需要将nameList映射到customerList,因此,您应该在nameList上进行流式传输并将其转换为customerList,而不是相反。接下来,您需要弄清楚如何使用一些外部方法/服务来获得额外的信息,如id、公司等,如果在map()操作期间需要的话。

相关内容

最新更新