代码
import vo.PosDataDetailVo;
import java.util.*;
import java.util.stream.Collectors;
public class AbcMain {
public static void main(String[] args) throws InterruptedException {
List list = new ArrayList();
PosDataDetailVo posDataDetailVo = new PosDataDetailVo();
posDataDetailVo.setAccountNo("张三");
posDataDetailVo.setBankCard("A1");
list.add(posDataDetailVo);
PosDataDetailVo posDataDetailVo2 = new PosDataDetailVo();
posDataDetailVo2.setAccountNo("");
posDataDetailVo2.setBankCard("B2");
list.add(posDataDetailVo2);
PosDataDetailVo posDataDetailVo3 = new PosDataDetailVo();
posDataDetailVo3.setAccountNo("李四");
posDataDetailVo3.setBankCard("C3");
list.add(posDataDetailVo3);
PosDataDetailVo posDataDetailVo4 = new PosDataDetailVo();
posDataDetailVo4.setAccountNo("李四");
posDataDetailVo4.setBankCard("D5");
list.add(posDataDetailVo4);
System.out.println("------------");
System.out.println("按照 AccountNo 去重");
List collect2 = list.stream().collect(Collectors.collectingAndThen(
Collectors.toCollection(
() -> new TreeSet(Comparator.comparing(PosDataDetailVo::getAccountNo))
), ArrayList::new)
);
for (PosDataDetailVo dataDetailVo : collect2) {
System.out.println(dataDetailVo);
}
System.out.println("------------");
Thread.sleep(2000);
System.out.println("循环出 AccountNo = 张三 的数据");
list.stream().filter(posDataDetailVo1 -> "张三".equals(posDataDetailVo1.getAccountNo())).forEach(posDataDetailVo1 -> {
System.out.println(posDataDetailVo1);
});
System.out.println("------------");
System.out.println("取出 AccountNo = 李四 的数据");
List collect = list.stream().filter(posDataDetailVo1 -> "李四".equals(posDataDetailVo1.getAccountNo())).collect(Collectors.toList());
for (PosDataDetailVo dataDetailVo : collect) {
System.out.println(dataDetailVo);
}
System.out.println("------------");
System.out.println("转为map,AccountNo为key,BankCard为value,并且如果key存在重复就取第二个数据");
Map collect1 = list.stream().collect(Collectors.toMap(PosDataDetailVo::getAccountNo, PosDataDetailVo::getBankCard, (v1, v2) -> v2));
System.out.println(collect1);
System.out.println("------------");
System.out.println("将AccountNo = 李四 的数据转为List");
//如果不需要过滤器,那么就删掉filter过滤器
List stringList = list.stream().filter(posDataDetailVo1 -> "李四".equals(posDataDetailVo1.getAccountNo())).map(PosDataDetailVo::getAccountNo).collect(Collectors.toList());
//也可以用下面这种方式,一样的
//List stringList = list.stream().filter(posDataDetailVo1 -> "李四".equals(posDataDetailVo1.getAccountNo())).map(posDataDetailVo1 -> posDataDetailVo1.getAccountNo()).collect(Collectors.toList());
for (String s : stringList) {
System.out.println(s);
}
System.out.println("------------");
System.out.println("List去重");
List myList= Arrays.asList("a","b","b","c","c","d");
List myListNew = myList.stream().distinct().collect(Collectors.toList());
System.out.println(myListNew);
System.out.println("------------");
//查找AccountNo=李四,并且BankCard=D5 的元素,存在返回true,不存在返回fasle
//anyMatch、allMatch、noneMatch自行百度有什么区别
boolean exist = list.stream().anyMatch(data -> {
boolean equals1 = "李四".equals(data.getAccountNo());
boolean equals2 = "D5".equals(data.getBankCard());
return equals1 && equals2;
});
//简写Lambda方式
//boolean exist = list.stream().anyMatch(data -> "李四".equals(data.getAccountNo()) && "D5".equals(data.getBankCard()));
System.out.println(exist);
}
}
运行结果
------------
按照 AccountNo 去重
PosDataDetailVo{accountNo='', bankCard='B2'}
PosDataDetailVo{accountNo='张三', bankCard='A1'}
PosDataDetailVo{accountNo='李四', bankCard='C3'}
------------
循环出 AccountNo = 张三 的数据
PosDataDetailVo{accountNo='张三', bankCard='A1'}
------------
取出 AccountNo = 李四 的数据
PosDataDetailVo{accountNo='李四', bankCard='C3'}
PosDataDetailVo{accountNo='李四', bankCard='D5'}
------------
转为map,AccountNo为key,BankCard为value,并且如果key存在重复就取第二个数据
{=B2, 李四=D5, 张三=A1}
------------
将AccountNo = 李四 的数据转为List
李四
李四
------------
List去重
[a, b, c, d]
------------
true
服务器托管,北京服务器托管,服务器租用 http://www.fwqtg.net
机房租用,北京机房租用,IDC机房托管, http://www.fwqtg.net
相关推荐: [Part2]JavaScript生态加速攻略:模块解析
本文首发于微信公众号:大迁世界, 我的微信:qq449245884,我会第一时间和你分享前端行业趋势,学习途径等等。更多开源作品请看 GitHub https://github.com/qq449245884/xiaozhi ,包含一线大厂面试完整考点、资料以…