1.判断是否存在某个值
//判断集合list中username是否存在张三这个值,存在返回true
boolean bool = list.stream().anyMatch(a->a.getUserName().equals("张三"));
2.过滤list中某个实体类的某个元素值
//过滤集合list中含有username为张三的值,结果集为过滤后的集合(全是包含张三的对象)
List<Userinfo> data = list.stream().filter(a->a.getUserName().equals("张三"))
.collect(Collectors.toList());
if(data!=null&&data.size()>0){
for (Userinfo userinfo:data) {
System.out.println(userinfo.getUserName()+"------------"+userinfo.getPassword());
}
}
3.替换list中某个实体类的某个元素值
Bean bean1 = new Bean(1, 2);
Bean bean2 = new Bean(3, 4);
List<Bean> list = Lists.newArrayList(bean1, bean2);
System.out.println("list1 : " + list);
list.stream().filter(bean -> {
if (1 == bean.getNum1())
bean.setNum1(2);
return true;
}).collect(Collectors.toList());
System.out.println("list2 : " + list);
4.收集集合中某个元素的值并逗号分割成字符串
String productIds=crmProductList.stream().map(p->p.getId()).collect(Collectors.joining(","));
5.list转map
//按照技能组把list转成map
Map<String, List<DefWorksheet>> mapList = listClaimOverDateWorksheet.stream().collect(Collectors.groupingBy(DefWorksheet::getProcessQueue));