java中两个list集合取并集、交集和差集
List<String> list1 = new ArrayList<>(); List<String> list2 = new ArrayList<>(); list1.add("A"); list1.add("C"); list1.add("D"); list2.add("A"); list2.add("B"); //获取两个集合并集 List<String> strings = new ArrayList<>(CollectionUtils.union(list1, list2)); System.out.println(strings); //[A, B, C, D] //获取两个集合交集 List<String> strings1 = new ArrayList<>(CollectionUtils.intersection(list1, list2)); System.out.println(strings1); //[A] //获取两个集合交集的补集 List<String> strings12 = new ArrayList<>(CollectionUtils.subtract(list1, list2)); System.out.println(strings12); //[C, D] //获取两个集合差集的补集 List<String> strings2 = new ArrayList<>(CollectionUtils.disjunction(list1, list2)); System.out.println(strings2); //[B, C, D]
对list数据进行筛选
//对相同内容的list进行去重 List<AppleNode> disList = appleList.stream().distinct().collect(Collectors.toList()); //取出name和code和apleNewList中的数据一致的内容 List<Apple> filterAppleList = appleNewList.stream().filter(apple-> name.equals(apple.getName()). filter(apple -> code.equals(apple.getCode())).collect(Collectors.toList()); //根据list中的内容取出最大值对应的实体 Apple filterHs = filterHistoryList.stream().max(Comparator.comparing(Apple:getNumbet).get();
标签:java,list1,list,ArrayList,List,差集,list2,new From: https://www.cnblogs.com/microsoft-xin/p/17040052.html