标签:assert username 最小 atm integer idcard sem desc map
//运算 现在想获取User的身份证号码 List<User> users; List<String> idcards= users.stream().map(User::getIdcard).collect(Collectors.toList()) 数组所有元素,按某种规律计算: List<Integer> num = Arrays.asList(1,2,3,4,5); List<Integer> collect1 = num.stream().map(n -> n * 2).collect(Collectors.toList()); System.out.println(collect1); List<String> list= Arrays.asList("a", "b", "c", "d"); List<String> collect =list.stream().map(String::toUpperCase).collect(Collectors.toList()); System.out.println(collect); //[A, B, C, D] //过滤 //所有名字叫hanmeimei的集合 List<Object> firstObject= ObjectList.stream() .filter(a -> "hanmeimei".equals(a.getUserName())) .collect(Collectors.toList()); //所有名字为空的集合 List<Object> firstObject= ObjectList.stream() .filter(a -> StringUtils.isEmpty(a.getUserName())).collect(Collectors.toList()); //排序 List<User> userss = users.stream().sorted(new Comparator<User>() { @Override public int compare(User u1, User u2) { return u1.getId() - u2.getId(); } }).collect(Collectors.toList()); //去重 .distinct() // 统计 总数 总和 最大 最小 平均 .mapToInt(i -> i).summaryStatistics() flatMapToInt //flatMap Stream<List<Integer>> stream = Stream.of(Arrays.asList(1,2), Arrays.asList(3, 4, 5)); stream.flatMap(list -> list.stream()).forEach(i -> System.out.println(i)); 实现List<Data1>和List<Data2>根据Id进行连接,将连接结果输出为一个List<Data3>: List<UserResult> userResultS = users.stream().flatMap(x -> userVos.stream() .filter(y -> x.getId() == y.getId()) .map(y -> new UserResult(x.getName(),x.getIdCard(),y.getNameVo(),y.getIdCardVo()))) .collect(Collectors.toList()); System.out.println(userResult Map<Integer, ListContainer> map = Maps.newHashMap(); List<AClass> aClassList1 = Lists.newArrayList(); AClass aClass = new AClass(1, "zhuoli1", "haha1"); aClassList1.add(aClass); aClassList1.add(new AClass(2, "zhuoli2", "haha2")); aClassList1.add(new AClass(3, "zhuoli3", "haha3")); List<AClass> aClassList2 = Lists.newArrayList(); aClassList2.add(aClass); aClassList2.add(new AClass(5, "zhuoli5", "haha5")); aClassList2.add(new AClass(6, "zhuoli6", "haha6")); /*交集*/ /*[AClass(id=1, name=zhuoli1, description=haha1)]*/ List<AClass> intersectResult = aClassList1.stream().filter(aClassList2::contains).collect(Collectors.toList()); System.out.println(intersectResult); /*并集*/ List<AClass> unionResult = Stream.of(aClassList1, aClassList2).flatMap(Collection::stream).distinct().collect(Collectors.toList()); assertEquals(unionResult.size(), 5); /*差集*/ /*[AClass(id=2, name=zhuoli2, description=haha2), AClass(id=3, name=zhuoli3, description=haha3)]*/ List<AClass> differenceResult = aClassList1.stream().filter(x -> !aClassList2.contains(x)).collect(Collectors.toList()); System.out.println(differenceResult); map.put(1, new ListContainer(aClassList1)); map.put(2, new ListContainer(aClassList2)); /*合并多个list*/ List<AClass> aClassListResult = map.values().stream().flatMap(listContainer -> listContainer.getLst().stream()).collect(Collectors.toList()); /*注意跟并集的区别*/ assertEquals(aClassListResult.size(), 6); System.out.println(aClassListResult);
标签:assert username 最小 atm integer idcard sem desc map
原文地址:https://www.cnblogs.com/xingminghui111/p/13619162.html