zoukankan      html  css  js  c++  java
  • java两个List的交集,并集

    java两个List的交集,并集,差异集

    public class Test {
    
        public static void main(String[] args) {
    
            List<String> list1 = new ArrayList<String>();
            list1.add("1111");
            list1.add("2222");
            list1.add("3333");
            list1.add("4444");
            list1.add("5555");
    
            List<String> list2 = new ArrayList<String>();
            list2.add("3333");
            list2.add("4444");
            list2.add("6666");
    
            Test test = new Test();
            List<String> list1_1 = test.deepCloneList(list1);
            list1_1.retainAll(list2); //list1与list2的交集
            System.out.println(list1_1.toString());//[3333, 4444]
    
            List<String> list1_2 = test.deepCloneList(list1);
            list1_2.removeAll(list2); //list1移除list2的集合
            System.out.println(list1_2.toString());//[1111, 2222, 5555]
    
            List<String> list2_1 = test.deepCloneList(list2);
            list2_1.removeAll(list1); //list2移除list1的集合
            System.out.println(list2_1.toString());//[6666]
    
            List<String> list1_3 = test.deepCloneList(list1);
            list1_3.addAll(list2);//list1_3与list2的并集
            System.out.println(list1_3.toString());//[1111, 2222, 3333, 4444, 5555, 3333, 4444, 6666]
    
    
        }
    
        /**
         * 对象深拷贝
         *
         * @param obj
         * @param <T>
         * @return
         */
        public static <T extends Serializable> T deepClone2(T obj) {
            T cloneObj = null;
            try {
                //写入字节流
                ByteArrayOutputStream out = new ByteArrayOutputStream();
                ObjectOutputStream obs = new ObjectOutputStream(out);
                obs.writeObject(obj);
                obs.close();
    
                //分配内存,写入原始对象,生成新对象
                ByteArrayInputStream ios = new ByteArrayInputStream(out.toByteArray());
                ObjectInputStream ois = new ObjectInputStream(ios);
                //返回生成的新对象
                cloneObj = (T) ois.readObject();
                ois.close();
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                //关闭流
            }
            return cloneObj;
        }
    
        /**
         * 对象深拷贝 try-with-resources写法
         *
         * @param obj
         * @param <T>
         * @return
         */
        public static <T extends Serializable> T deepClone(T obj) {
            T cloneObj = null;
            try (ByteArrayOutputStream out = new ByteArrayOutputStream();
                 ObjectOutputStream obs = new ObjectOutputStream(out);
                 ByteArrayInputStream ios = new ByteArrayInputStream(out.toByteArray());
                 ObjectInputStream ois = new ObjectInputStream(ios);
            ) {
                obs.writeObject(obj);
                cloneObj = (T) ois.readObject();
            } catch (IOException e) {
                e.printStackTrace();
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            }
            return cloneObj;
        }
    
        /**
         * List深拷贝
         *
         * @param src
         * @param <T>
         * @return
         */
        public static <T> List<T> deepCloneList2(List<T> src) {
            List<T> dest = null;
            try {
                ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
                ObjectOutputStream out = new ObjectOutputStream(byteOut);
                out.writeObject(src);
    
                ByteArrayInputStream byteIn = new ByteArrayInputStream(byteOut.toByteArray());
                ObjectInputStream in = new ObjectInputStream(byteIn);
                dest = (List<T>) in.readObject();
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                //关闭流
            }
            return dest;
        }
    
        /**
         * List深拷贝 try-with-resources写法
         *
         * @param src
         * @param <T>
         * @return
         */
        public static <T> List<T> deepCloneList(List<T> src) {
            List<T> dest = null;
            try {
                ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
                ObjectOutputStream out = new ObjectOutputStream(byteOut);
                out.writeObject(src);
    
                ByteArrayInputStream byteIn = new ByteArrayInputStream(byteOut.toByteArray());
                ObjectInputStream in = new ObjectInputStream(byteIn);
                dest = (List<T>) in.readObject();
            } catch (Exception e) {
                e.printStackTrace();
            }return dest;
        }
    
    
    }
  • 相关阅读:
    【Leetcode】【Easy】Remove Duplicates from Sorted List
    【Leetcode】【Easy】Pascal's Triangle II
    【Leetcode】【Easy】Pascal's Triangle
    【Leetcode】【Easy】Binary Tree Level Order Traversal II
    【Leetcode】【Easy】Binary Tree Level Order Traversal
    【Leetcode】【Easy】Maximum Depth of Binary Tree
    【Leetcode】【Easy】Minimum Depth of Binary Tree
    【Leetcode】【Easy】Balanced Binary Tree
    【Leetcode】【Easy】Symmetric Tree
    如何使用Action.Invoke()触发一个Storyboard
  • 原文地址:https://www.cnblogs.com/ooo0/p/13091760.html
Copyright © 2011-2022 走看看