例,用户1有“admin”,"test"两个角色,用户2有“test”,
上代码
List<TestUser> list = new ArrayList<>();标签:user2,stream,Stream,user1,list,合并,List,TestUser From: https://www.cnblogs.com/huangtiing/p/16918428.html
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);
});