public static void main(String[] args) {
List<String> list1 = new ArrayList<String>();
list1.add("1");
list1.add("2");
list1.add("3");
list1.add("5");
list1.add("6");
List<String> list2 = new ArrayList<String>();
list2.add("2");
list2.add("3");
list2.add("7");
list2.add("8");
// 交集
List<String> intersection = list1.stream().filter(item -> list2.contains(item)).collect(toList());
System.out.println("---交集 intersection---");
intersection.parallelStream().forEach(System.out :: println);
// 差集 (list1 - list2)
List<String> reduce1 = list1.stream().filter(item -> !list2.contains(item)).collect(toList());
System.out.println("---差集 reduce1 (list1 - list2)---");
reduce1.parallelStream().forEach(System.out :: println);
// 差集 (list2 - list1)
List<String> reduce2 = list2.stream().filter(item -> !list1.contains(item)).collect(toList());
System.out.println("---差集 reduce2 (list2 - list1)---");
reduce2.parallelStream().forEach(System.out :: println);
// 并集
List<String> listAll = list1.parallelStream().collect(toList());
List<String> listAll2 = list2.parallelStream().collect(toList());
listAll.addAll(listAll2);
System.out.println("---并集 listAll---");
listAll.parallelStream().forEachOrdered(System.out :: println);
// 去重并集
List<String> listAllDistinct = listAll.stream().distinct().collect(toList());
System.out.println("---得到去重并集 listAllDistinct---");
listAllDistinct.parallelStream().forEachOrdered(System.out :: println);
System.out.println("---原来的List1---");
list1.parallelStream().forEachOrdered(System.out :: println);
System.out.println("---原来的List2---");
list2.parallelStream().forEachOrdered(System.out :: println);
}
public static void main(String[] args) {
// 老师集合
List<Teacher> teachers = Arrays.asList(
new Teacher(1L, "张三"),
new Teacher(2L, "李四"),
new Teacher(3L, "王五"),
new Teacher(4L, "赵六"));
// 学生集合
List<Student> students = Arrays.asList(
new Student(5L, "张三"),
new Student(6L, "李四"),
new Student(7L, "小红"),
new Student(8L, "小明"));
// 求同时出现在老师集合和学生集合中的人数,name相同即视为同一个人
int size = teachers.stream()
.map(t -> students.stream().filter(s -> Objects.nonNull(t.getName()) && Objects.nonNull(s.getName()) && Objects.equals(t.getName(), s.getName())).findAny().orElse(null))
.filter(Objects::nonNull)
.collect(Collectors.toList())
.size();
// 求同时出现在老师集合和学生集合中人的name集合,name相同即视为同一个人
List<String> names = teachers.stream()
.map(t -> students.stream().filter(s -> Objects.nonNull(t.getName()) && Objects.nonNull(s.getName()) && Objects.equals(t.getName(), s.getName())).findAny().orElse(null))
.filter(Objects::nonNull)
.map(r -> r.getName())
.collect(Collectors.toList());
System.out.println("相同的人数:" + size);
System.out.println("相同的人姓名集合:" + names);
}
//java8 lambda 统计list中对象的重复次数
public static void main(String[] args) {
List<String> list = new ArrayList() {
{
add("12");
add("20");
add("12");
add("22");
add("22");
add("23");
add("159");
add("12");
}
};
Map<String, Long> map = list.stream()
.collect(Collectors.groupingBy(p -> p, Collectors.counting()));
map.forEach((k, v) -> System.out.println(k + ":" + v));
//v:重复的次数 k重复的值
map.forEach((k, v) ->{
if(v==list.size()){
System.out.println("重复的元素"+k);
}
});
}
java8两个List集合取交集、并集、差集、去重并集,统计list中对象的重复次数
于 2020-05-21 17:46:27 首次发布