java8+list+交集+并集_New 采用java8 lambda表达式 实现 java list 交集 并集 差集 去重复并集...

package com.wxx.webbase;

import java.util.*;

import static java.util.stream.Collectors.toList;

/** * @author admin */

public class MyTest {

/** * 用于测试的对象类 */

static class Student {

/** * 姓名 */

private String name;

/** * 学号 唯一值 */

private String code;

public Student(String name, String code) {

this.name = name;

this.code = code;

}

@Override

public boolean equals(Object o) {

if (this == o) {

return true;

}

if (!(o instanceof Student)) {

return false;

}

Student student = (Student) o;

return code.equals(student.getCode());

}

@Override

public int hashCode() {

return Objects.hash(code);

}

@Override

public String toString() {

return "Student{" +

"name='" + name + '\'' +

", code='" + code + '\'' +

'}';

}

public String getName() {

return name;

}

public String getCode() {

return code;

}

}

/** * 对象类型的处理 */

public static void showObjectDeal() {

List list1 = new ArrayList<>();

list1.add(new Student("name1","1001"));

list1.add(new Student("name2","1002"));

list1.add(new Student("name3","1003"));

List list2 = new ArrayList<>();

list2.add(new Student("name3","1003"));

list2.add(new Student("name4","1004"));

Set list1Set = new HashSet<>(list1);

Set list2Set = new HashSet<>(list2);

// 交集

List intersection = list1.stream().filter(list2Set::contains).collect(toList());

System.out.println("---得到交集 intersection---");

intersection.parallelStream().forEach(System.out::println);

// 差集 (list1 - list2)

List reduce1 = list1.stream().filter(item -> !list2Set.contains(item)).collect(toList());

System.out.println("---得到差集 reduce1 (list1 - list2)---");

reduce1.parallelStream().forEach(System.out::println);

// 差集 (list2 - list1)

List reduce2 = list2.stream().filter(item -> !list1Set.contains(item)).collect(toList());

System.out.println("---得到差集 reduce2 (list2 - list1)---");

reduce2.parallelStream().forEach(System.out::println);

// 并集

List listAll = list1.parallelStream().collect(toList());

List listAll2 = list2.parallelStream().collect(toList());

listAll.addAll(listAll2);

System.out.println("---得到并集 listAll---");

listAll.parallelStream().forEach(System.out::println);

// 去重并集

list1Set.addAll(list2Set);

List listDistinctAll = new ArrayList<>(list1Set);

System.out.println("---得到去重并集 listDistinctAll---");

listDistinctAll.parallelStream().forEach(System.out::println);

System.out.println("---原来的List1---");

list1.parallelStream().forEach(System.out::println);

System.out.println("---原来的List2---");

list2.parallelStream().forEach(System.out::println);

}

/** * 简单类型的处理 */

public static void showSimpleDeal() {

List list1 = new ArrayList<>();

list1.add("1111");

list1.add("2222");

list1.add("3333");

List list2 = new ArrayList<>();

list2.add("3333");

list2.add("4444");

Set list1Set = new HashSet<>(list1);

Set list2Set = new HashSet<>(list2);

// 交集

List intersection = list1.stream().filter(list2Set::contains).collect(toList());

System.out.println("---得到交集 intersection---");

intersection.parallelStream().forEach(System.out::println);

// 差集 (list1 - list2)

List reduce1 = list1.stream().filter(item -> !list2Set.contains(item)).collect(toList());

System.out.println("---得到差集 reduce1 (list1 - list2)---");

reduce1.parallelStream().forEach(System.out::println);

// 差集 (list2 - list1)

List reduce2 = list2.stream().filter(item -> !list1Set.contains(item)).collect(toList());

System.out.println("---得到差集 reduce2 (list2 - list1)---");

reduce2.parallelStream().forEach(System.out::println);

// 并集

List listAll = list1.parallelStream().collect(toList());

List listAll2 = list2.parallelStream().collect(toList());

listAll.addAll(listAll2);

System.out.println("---得到并集 listAll---");

listAll.parallelStream().forEach(System.out::println);

// 去重并集

list1Set.addAll(list2Set);

List listDistinctAll = new ArrayList<>(list1Set);

System.out.println("---得到去重并集 listDistinctAll---");

listDistinctAll.parallelStream().forEach(System.out::println);

System.out.println("---原来的List1---");

list1.parallelStream().forEach(System.out::println);

System.out.println("---原来的List2---");

list2.parallelStream().forEach(System.out::println);

}

public static void main(String[] args) {

// 基本类型测试

showSimpleDeal();

// 对象测试

showObjectDeal();

// 一般有filter 操作时,不用并行流parallelStream ,如果用的话可能会导致线程安全问题 !

// parallelStream 由于是并行执行, 输出可能和list本身的顺序不一样 !

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值