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

一般的javaList 交、并集采用简单的 removeAll retainAll 等操作,不过这也破坏了原始的javaList对象,采用java8 lambda表达式流操作则可以不影响原始list对象而得到两个javaList对象的 交、并、差集。

之前写过一篇文章,采用java8 lambda表达式 实现 java list 交集 并集 差集 去重复并集 当时没有考虑到性能和 对一般对象的处理,这里做下日志说明一下:

原来文章 https://blog.csdn.net/gzt19881123/article/details/78327465

改进后的代码


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<Student> list1 = new ArrayList<>();
        list1.add(new Student("name1","1001"));
        list1.add(new Student("name2","1002"));
        list1.add(new Student("name3","1003"));

        List<Student> list2 = new ArrayList<>();
        list2.add(new Student("name3","1003"));
        list2.add(new Student("name4","1004"));

        Set<Student> list1Set = new HashSet<>(list1);

        Set<Student> list2Set = new HashSet<>(list2);

        // 交集
        List<Student> intersection = list1.stream().filter(list2Set::contains).collect(toList());
        System.out.println("---得到交集 intersection---");
        intersection.parallelStream().forEach(System.out::println);

        // 差集 (list1 - list2)
        List<Student> 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<Student> reduce2 = list2.stream().filter(item -> !list1Set.contains(item)).collect(toList());
        System.out.println("---得到差集 reduce2 (list2 - list1)---");
        reduce2.parallelStream().forEach(System.out::println);

        // 并集
        List<Student> listAll = list1.parallelStream().collect(toList());
        List<Student> listAll2 = list2.parallelStream().collect(toList());
        listAll.addAll(listAll2);
        System.out.println("---得到并集 listAll---");
        listAll.parallelStream().forEach(System.out::println);

        // 去重并集
        list1Set.addAll(list2Set);
        List<Student> 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<String> list1 = new ArrayList<>();
        list1.add("1111");
        list1.add("2222");
        list1.add("3333");

        List<String> list2 = new ArrayList<>();
        list2.add("3333");
        list2.add("4444");

        Set<String> list1Set = new HashSet<>(list1);

        Set<String> list2Set = new HashSet<>(list2);

        // 交集
        List<String> intersection = list1.stream().filter(list2Set::contains).collect(toList());
        System.out.println("---得到交集 intersection---");
        intersection.parallelStream().forEach(System.out::println);

        // 差集 (list1 - list2)
        List<String> 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<String> reduce2 = list2.stream().filter(item -> !list1Set.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().forEach(System.out::println);

        // 去重并集
        list1Set.addAll(list2Set);
        List<String> 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本身的顺序不一样 !
    }
}


  • 10
    点赞
  • 20
    收藏
    觉得还不错? 一键收藏
  • 10
    评论
1. 可以使用容器的构造函数来初始化容器,例如: ```c++ int arr[] = {1, 2, 3, 4, 5}; vector<int> v(arr, arr + 5); ``` 这里使用了 vector 的构造函数,第一个参数表示起始地址,第二个参数表示结束地址。 2. 迭代器是一个用来遍历容器中元素的对象。前插型迭代器 front_insert_iterator 和后插型迭代器 back_insert_iterator 用来在容器的前端和后端插入元素。使用前插型迭代器和后插型迭代器需要满足容器支持插入操作,例如 vector、deque、list 等。定义方法如下: ```c++ #include <iterator> #include <vector> #include <iostream> using namespace std; int main() { vector<int> v; front_insert_iterator<vector<int>> fii(v); back_insert_iterator<vector<int>> bii(v); for (int i = 0; i < 5; i++) { *fii = i; *bii = i; } for (auto x : v) { cout << x << " "; } cout << endl; return 0; } ``` 3. for_each 算法可以用来访问容器中的元素,需要传入一个函数对象作为参数。函数对象可以是一个函数指针、函数对象、lambda 表达式等。例如: ```c++ #include <vector> #include <algorithm> #include <iostream> using namespace std; void print(int x) { cout << x << " "; } int main() { vector<int> v = {1, 2, 3, 4, 5}; for_each(v.begin(), v.end(), print); cout << endl; return 0; } ``` 4. copy_if 算法用于将满足条件的元素拷贝到另一个容器中,需要传入源容器的起始和结束位置、目标容器的起始位置、以及一个谓词函数作为参数。例如: ```c++ #include <vector> #include <algorithm> #include <iostream> using namespace std; bool is_odd(int x) { return x % 2 == 1; } int main() { vector<int> v = {1, 2, 3, 4, 5}; vector<int> v2; copy_if(v.begin(), v.end(), back_inserter(v2), is_odd); for (auto x : v2) { cout << x << " "; } cout << endl; return 0; } ``` 5. 自定义类型可以重载比较运算符(<、==、> 等)和流插入运算符(<<),以便在容器中进行排序、输出等操作。重载的目的是为了支持自定义类型的比较和输出。例如: ```c++ #include <iostream> #include <string> using namespace std; class Student { public: string name; int score; bool operator < (const Student& other) const { return score < other.score; } bool operator == (const Student& other) const { return score == other.score; } bool operator > (const Student& other) const { return score > other.score; } friend ostream& operator << (ostream& os, const Student& s) { os << s.name << ": " << s.score; return os; } }; int main() { Student s1 = {"Tom", 80}; Student s2 = {"Jerry", 90}; cout << (s1 < s2) << endl; cout << (s1 == s2) << endl; cout << (s1 > s2) << endl; cout << s1 << endl; return 0; } ``` 6. 自定义类型可以使用 max_element、min_element、max、min 等算法进行操作,前提是要满足比较运算符的定义。例如: ```c++ #include <vector> #include <algorithm> #include <iostream> using namespace std; class Student { public: string name; int score; bool operator < (const Student& other) const { return score < other.score; } bool operator == (const Student& other) const { return score == other.score; } bool operator > (const Student& other) const { return score > other.score; } }; int main() { vector<Student> v = {{"Tom", 80}, {"Jerry", 90}, {"Alice", 70}}; auto it = max_element(v.begin(), v.end()); cout << *it << endl; return 0; } ``` 7. generate 算法可以用来生成容器中的元素,需要传入一个函数对象作为参数。函数对象可以是一个函数指针、函数对象、lambda 表达式等。例如: ```c++ #include <vector> #include <algorithm> #include <iostream> using namespace std; int fib(int n) { if (n == 0 || n == 1) { return 1; } return fib(n - 1) + fib(n - 2); } int main() { vector<int> v(10); generate(v.begin(), v.end(), []() {return rand() % 100;}); for (auto x : v) { cout << x << " "; } cout << endl; generate(v.begin(), v.end(), fib); for (auto x : v) { cout << x << " "; } cout << endl; return 0; } ``` 8. remove 和 remove_if 算法用于将满足条件的元素移动到容器的末尾,并返回移动后的末尾位置。需要使用 erase 算法来真正删除元素。例如: ```c++ #include <vector> #include <algorithm> #include <iostream> using namespace std; bool is_odd(int x) { return x % 2 == 1; } int main() { vector<int> v = {1, 2, 3, 4, 5}; auto it = remove(v.begin(), v.end(), 3); v.erase(it, v.end()); for (auto x : v) { cout << x << " "; } cout << endl; vector<int> v2 = {1, 2, 3, 4, 5}; auto it2 = remove_if(v2.begin(), v2.end(), is_odd); v2.erase(it2, v2.end()); for (auto x : v2) { cout << x << " "; } cout << endl; return 0; } ``` 9. 有序序列的并集交集差集等运算可以使用 set 的集合运算操作来实现。例如: ```c++ #include <set> #include <algorithm> #include <iostream> using namespace std; int main() { set<int> s1 = {1, 2, 3, 4}; set<int> s2 = {3, 4, 5, 6}; set<int> s3; set_intersection(s1.begin(), s1.end(), s2.begin(), s2.end(), inserter(s3, s3.begin())); for (auto x : s3) { cout << x << " "; } cout << endl; set_union(s1.begin(), s1.end(), s2.begin(), s2.end(), inserter(s3, s3.begin())); for (auto x : s3) { cout << x << " "; } cout << endl; set_difference(s1.begin(), s1.end(), s2.begin(), s2.end(), inserter(s3, s3.begin())); for (auto x : s3) { cout << x << " "; } cout << endl; return 0; } ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 10
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值