zoukankan      html  css  js  c++  java
  • 高效的找出两个List中的不同元素

     

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    /*
    * TestList.java
    * Version 1.0.0
    * Created on 2017年12月15日
    * Copyright ReYo.Cn
    */
    package reyo.sdk.utils.test.list2;
     
    import java.util.ArrayList;
    import java.util.HashMap;
    import java.util.List;
    import java.util.Map;
     
    public class TestList {
     
        public static void main(String[] args) {
            List<String> list1 = new ArrayList<String>();
            List<String> list2 = new ArrayList<String>();
            for (int i = 0; i < 10000; i++) {
                list1.add("test" + i);
                list2.add("test" + i * 2);
            }
            getDiffrent(list1, list2);
            getDiffrent3(list1, list2);
            getDiffrent5(list1, list2);
            getDiffrent4(list1, list2);
            getDiffrent2(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;
            }
            Map<String, Integer> map = new HashMap<String, Integer>(maxList.size());
            for (String string : maxList) {
                map.put(string, 1);
            }
            for (String string : minList) {
                if (map.get(string) != null) {
                    map.put(string, 2);
                    continue;
                }
                diff.add(string);
            }
            for (Map.Entry<String, Integer> entry : map.entrySet()) {
                if (entry.getValue() == 1) {
                    diff.add(entry.getKey());
                }
            }
            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;
        }
    }

     getDiffrent total times 320118400
    getDiffrent3 total times 12051500
    getDiffrent5 total times 6301100
    getDiffrent4 total times 7527300
    getDiffrent2 total times 298880900

  • 相关阅读:
    PHP迭代生成器---yield
    array_chunk — 将一个数组分割成多个
    php array_change_key_case
    PHP trait介绍
    mysql视图
    mysql常见内置函数
    MySQL表复制
    二分查找算法(折半查找算法)
    使用SplFixedArray创建固定大小的数组
    Frameset Example
  • 原文地址:https://www.cnblogs.com/lemperor/p/15308685.html
Copyright © 2011-2022 走看看