使⽤Lambda表达式.map循环遍历List集合赋值使⽤Lambda表达式给对象赋值
0. 技术点
使⽤Lambda表达式.map循环遍历List集合赋值
1. 实体类
@Data
public class InfoNoticeItem implements Serializable {
/**
* serialVersionUID
*/
private static final long serialVersionUID = 6411598034131994800L;
/**
* ids 商品内码
*/
private String ids;
/**
* storeCode 门店编号
*/
lua字符串转数组
private String storeCode;
}
2. 测试类
import com.alibaba.fastjson.JSON;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;
public class Test {
public static void main(String[] args) {
InfoNoticeItem re2 = new InfoNoticeItem();
re2.setIds("2");
re2.setStoreCode("⾼佳琪");
InfoNoticeItem re4 = new InfoNoticeItem();
re4.setIds("4");
re4.setStoreCode("王莹莹");
List<InfoNoticeItem> list = new ArrayList<>();
Collections.addAll(list, re2, re4);
ShopInfoNoticeReq shopInfoNoticeReq = new ShopInfoNoticeReq();
shopInfoNoticeReq.setFormat(null);
shopInfoNoticeReq.setIdList(list);
// 赋值.setIdList(collect)
// public void setIdList(List<InfoNoticeItem> idList)
List<InfoNoticeItem> collect = IdList().stream().map(item -> {
InfoNoticeItem ini = new InfoNoticeItem();
ini.Ids());
ini.StoreCode());
return ini;
}).List());
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.JSON(collect));
//TODO 输出:[{"ids":"2","storeCode":"⾼佳琪"},{"ids":"4","storeCode":"王莹莹"}]
}
}
简单使⽤
ArrayList<Student> list = new ArrayList<>();
Collections.addAll(list, new Student(0, "张三"), new Student(10, "李四"));
System.out.println(list.stream().map(value -> Id()).List()));  System.out.println(list.stream().map(Student::getId).List()));
//输出: [0, 10]
//输出: [0, 10]

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。