Java 两个List集合取交集 并集
/** * @Description: 取交集 * @Param: [list1, list2] * @Return: java.util.List<java.lang.String> * @Author: * @Date: 2022/6/28 15:35 **/ public List<String> getAndRes(List<String> list1, List<String> list2) { if (null != list1 && list1.size() != 0) { if (null == list2 || list2.size() == 0) { return list1; } else { return list1.stream().filter(list2::contains).collect(Collectors.toList()); } } else { return list2; } }
/** * @Description: 取并集 * @Param: [list1, list2] * @Return: java.util.List<java.lang.String> * @Author: * @Date: 2022/6/28 15:35 **/ public List<String> getORRes(List<String> list1, List<String> list2) { list1.addAll(list2); return list1.stream().distinct().collect(Collectors.toList()); }
凡所有相,皆是虚妄