多个List,合并,去重操作。用stream , list 转map
例,用户1有“admin”,"test"两个角色,用户2有“test”,
上代码
List<TestUser> list = new ArrayList<>();
TestUser user1 = new TestUser();
user1.setRoles(Arrays.asList("admin","test"));
list.add(user1);
TestUser user2 = new TestUser();
user2.setRoles(Arrays.asList("admin"));
list.add(user2);
//Stream flatMap(Function<? super T, ? extends Stream<? extends R>> mapper)
//转换流并合并,将原Stream中的每个元素都转换成另一个Stream,然后把所有Stream连接成一个新的Stream。接收一个Function函数作为参数。
Stream<String> stringStream = list.stream().flatMap(i -> i.getRoles().stream());
stringStream.distinct().forEach(i ->{
System.out.println(i);
});
2 list转map
Map<String, SubsidyDataDetailModel> dataDetailMap = dataDetailList.stream().collect(Collectors.toMap(SubsidyDataDetailModel::getIdcard, Function.identity()));