获取两个List的不同元素

获取两个List的不同元素

/** 

 * 几种不同的方法,查询效率不同
 *getDiffrent5()效率最高
 */  

import java.util.ArrayList;  
import java.util.HashMap;  
import java.util.List;  
import java.util.Map;  
  
/** 
 * 获取两个List集合中的不相同的对象 
 */  
public class Test2ListDiff {  
  
    public static void main(String[] args) {  
        List<String> list1 = new ArrayList<String>();  
        List<String> list2 = new ArrayList<String>();  
        int num = 5;  
        // int num=5000;   
        for (int i = 0; i < num; i++) {  
            list1.add("test_" + i);  
            list2.add("test_" + i * 2);  
        }  
  
        getDiffrent3(list1, list2);  
        getDiffrent5(list1, list2);  
        getDiffrent4(list1, list2);  
        getDiffrent2(list1, list2);  
        getDiffrent(list1, list2);  
  
        // getDiffrent3 total times 32271699   
        // getDiffrent5 total times 12239545   
        // getDiffrent4 total times 16786491   
        // getDiffrent2 total times 2438731459   
    }  
  
    /** 
     * 获取两个List的不同元素 
     *  
     * @param list1 
     * @param list2 
     * @return 
     */  
    private static List<String> getDiffrent5(List<String> list1, List<String> list2) {  
        long st = System.nanoTime();  
        List<String> diff = new ArrayList<String>();  
        List<String> maxList = list1;  
        List<String> minList = list2;  
        if (list2.size() > list1.size()) {  
            maxList = list2;  
            minList = list1;  
        }  
  
        // 将List中的数据存到Map中   
        Map<String, Integer> maxMap = new HashMap<String, Integer>(maxList.size());  
        for (String string : maxList) {  
            maxMap.put(string, 1);  
        }  
  
        // max:1,2,3,4,5   
        // min:2,4,6,8,10   
  
        // 循环minList中的值,标记 maxMap中 相同的 数据2   
        for (String string : minList) {  
            // 相同的   
            if (maxMap.get(string) != null) {  
                maxMap.put(string, 2);  
                continue;  
            }  
            // 不相等的   
            diff.add(string);  
        }  
        printf(diff);  
  
        // 循环maxMap   
        for (Map.Entry<String, Integer> entry : maxMap.entrySet()) {  
            if (entry.getValue() == 1) {  
                diff.add(entry.getKey());  
            }  
        }  
        printf(diff);  
  
        System.out.println("getDiffrent5 total times " + (System.nanoTime() - st));  
        return diff;  
    }  
  
    /** 
     * 获取两个List的不同元素 
     *  
     * @param list1 
     * @param list2 
     * @return 
     */  
    private static List<String> getDiffrent4(List<String> list1, List<String> list2) {  
        long st = System.nanoTime();  
        Map<String, Integer> map = new HashMap<String, Integer>(list1.size() + list2.size());  
        List<String> diff = new ArrayList<String>();  
        List<String> maxList = list1;  
        List<String> minList = list2;  
        if (list2.size() > list1.size()) {  
            maxList = list2;  
            minList = list1;  
        }  
        for (String string : maxList) {  
            map.put(string, 1);  
        }  
        for (String string : minList) {  
            Integer cc = map.get(string);  
            if (cc != null) {  
                map.put(string, ++cc);  
                continue;  
            }  
            map.put(string, 1);  
        }  
        for (Map.Entry<String, Integer> entry : map.entrySet()) {  
            if (entry.getValue() == 1) {  
                diff.add(entry.getKey());  
            }  
        }  
        System.out.println("getDiffrent4 total times " + (System.nanoTime() - st));  
        return diff;  
    }  
  
    /** 
     * 获取两个List的不同元素 
     *  
     * @param list1 
     * @param list2 
     * @return 
     */  
    private static List<String> getDiffrent3(List<String> list1, List<String> list2) {  
        long st = System.nanoTime();  
        Map<String, Integer> map = new HashMap<String, Integer>(list1.size() + list2.size());  
        List<String> diff = new ArrayList<String>();  
        for (String string : list1) {  
            map.put(string, 1);  
        }  
        for (String string : list2) {  
            Integer cc = map.get(string);  
            if (cc != null) {  
                map.put(string, ++cc);  
                continue;  
            }  
            map.put(string, 1);  
        }  
        for (Map.Entry<String, Integer> entry : map.entrySet()) {  
            if (entry.getValue() == 1) {  
                diff.add(entry.getKey());  
            }  
        }  
        System.out.println("getDiffrent3 total times " + (System.nanoTime() - st));  
        return diff;  
    }  
  
    /** 
     * 获取连个List的不同元素 
     *  
     * @param list1 
     * @param list2 
     * @return 
     */  
    private static List<String> getDiffrent2(List<String> list1, List<String> list2) {  
        long st = System.nanoTime();  
        list1.retainAll(list2);  
        System.out.println("getDiffrent2 total times " + (System.nanoTime() - st));  
        return list1;  
    }  
  
    /** 
     * 获取两个List的不同元素 
     *  
     * @param list1 
     * @param list2 
     * @return 
     */  
    private static List<String> getDiffrent(List<String> list1, List<String> list2) {  
        long st = System.nanoTime();  
        List<String> diff = new ArrayList<String>();  
        for (String str : list1) {  
            if (!list2.contains(str)) {  
                diff.add(str);  
            }  
        }  
        System.out.println("getDiffrent total times " + (System.nanoTime() - st));  
        return diff;  
    }  
  
    public static void printf(List<String> list) {  
        System.out.println("----------------------------");  
        for (int i = 0; i < list.size(); i++) {  
            System.out.println(list.get(i));  
        }  
    }
时间: 2024-08-30 13:31:02

获取两个List的不同元素的相关文章

javascript 常见数组操作( 1、数组整体元素修改 2、 数组筛选 3、jquery 元素转数组 4、获取两个数组中相同部分或者不同部分 5、数组去重并倒序排序 6、数组排序 7、数组截取slice 8、数组插入、删除splice(需明确位置) 9、数组遍历 10、jQuery根据元素值删除数组元素的方)

主要内容: 1.数组整体元素修改 2. 数组筛选 3.jquery 元素转数组 4.获取两个数组中相同部分或者不同部分 5.数组去重并倒序排序 6.数组排序 7.数组截取slice 8.数组插入.删除splice(需明确位置) 9.数组遍历 10.jQuery根据元素值删除数组元素的方法 数组常见操作包含了 增.删.查.改.插入.交集.并集 1.数组整体元素修改 //map,给数组每个元素加1 输出[1,2,3] $.map([0,1,2],function(n){ return n+1; })

获取两个数组相同元素,不同元素,及相同元素不同元素新数组

废话不多说直接上代码! /** * 获取两个数组相同元素 * @param a * @param b * @return */ public static <T> Set<T> getIds(T[] a, T[] b){ Set<T> same = new HashSet<T>(); //用来存放两个数组中相同的元素 Set<T> temp = new HashSet<T>(); //用来存放数组a中的元素 for (int i =

jQuery -&gt; 获取/设置/删除DOM元素的属性

Sum square difference Problem 6 The sum of the squares of the first ten natural numbers is, 12 + 22 + ... + 102 = 385 The square of the sum of the first ten natural numbers is, (1 + 2 + ... + 10)2 = 552 = 3025 Hence the difference between the sum of

C# Linq获取两个List或数组的差集交集

List<int> list1 = new List<int>(); list1.Add(1); list1.Add(2); list1.Add(3); List<int> list2 = new List<int>(); list2.Add(3); list2.Add(4); list2.Add(5); //得到的结果是4,5 即减去了相同的元素. List<int> list3 = list2.Except(list1).ToList();

图算法 - 只需“五步” ,获取两节点间的所有路径(非递归方式)

在实现 “图” 数据结构时,会遇到 “获取两点之间是所有路径” 这个算法问题,网上的资料大多都是利用递归算法来实现(见文末的参考文章). 我们知道在 JS 中用递归算法很容易会让调用栈溢出,为了能在生产环境中使用,必须要用非递归方式的去实现. 经过一番探索,实现的思路主要来自文章 <求两点间所有路径的遍历算法> ,只是该文中并没有给出具体的实现细节,需要自己去实现:最终本文的实现结合类似<算法 - 调度场算法(Shunting Yard Algorithm)> 中所提及的双栈来完成

获取两个字符串中最大相同子串

2.获取两个字符串中最大相同子串.第一个动作:将短的那个串进行长度一次递减的子串打印. "cvhellobnmtanop" "andefc" 思路: 1,将短的那个子串按照长度递减的方式获取到. 2,将每获取到的子串去长串中判断是否包含,如果包含,已经找到! package tan; class Test { public static String getMaxSubString(String s1,String s2) { String max = "

数据结构中线性表的基本操作-合并两个线性表-依照元素升序排列

#include<iostream> #include<stdlib.h> #define LIST_INIT_SIZE 10/*线性表初始长度*/ #define LIST_CREATENT 2/*每次的增量*/ typedef int ElemType; using namespace std; typedef struct SqList/*线性表的数据结构定义*/ { ElemType *elem;/*线性表基址*/ int length;/*当前线性表所含的元素个数*/ i

数组-06. 找出不是两个数组共有的元素

数组-06. 找出不是两个数组共有的元素(20) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 张彤彧(浙江大学) 给定两个整型数组,本题要求找出不是两者共有的元素. 输入格式: 输入分别在2行中给出2个整型数组,每行先给出正整数N(<=20),随后是N个整数,其间以空格分隔. 输出格式: 在一行中按照数字给出的顺序输出不是两数组共有的元素,数字间以空格分隔,但行末不得有多余的空格.题目保证至少存在一个这样的数字.同一数字不重复

两个List去掉重复元素放在一个List中去【两个Listsize值非常大】

/* * 思路: * 1.取得两个list的相同元素:list.retainAll(E)方法 * 2.两个list分别去掉相同的元素:list.removeAll(E); * 3.将剩下的两个List分别加入到一个新的List去:addAll */ public class Test1 { public static void main(String[] args) { List list1 = new ArrayList(); Random random = new Random(); //l