本文共 4988 字,大约阅读时间需要 16 分钟。
第一种方式使用工具类进行比较
package com.hzbank.counter.utils;import java.util.ArrayList;import java.util.HashMap;import java.util.List;import java.util.Map;/** * lists数据比对 * @author nick */public class ListUtils { /* public static void main(String[] args) { Listlist1 = new ArrayList (); List list2 = new ArrayList (); for (int i = 0; i < 30000; i++) { list1.add("test" + i); } for (int i = 0; i < 80000; i++) { list2.add("test" + i * 2); } getDiffrentOne(list1, list2); getDiffrentTwo(list1, list2); getDiffrentThree(list1, list2); getDiffrentFour(list1, list2); }*/ // 方法1,两层遍历查找,遍历次数为list1.size()*list2.size(),有点蠢 private static List getDiffrentOne(List list1, List list2) { // diff 存放不同的元素 List diff = new ArrayList (); // 开始查找的时间,用于计时 /*long start = System.currentTimeMillis();*/ for (String str : list1) { if (!list2.contains(str)) { diff.add(str); } } // 计时 /*System.out.println("方法1 耗时:" + (System.currentTimeMillis() - start) + " 毫秒");*/ return diff; } // 方法2,两层遍历查找,用retainAll()方法查找,也很蠢,方法底层依旧是两层遍历 private static List getDiffrentTwo(List list1, List list2) { /*long start = System.currentTimeMillis();*/ list1.retainAll(list2);// 返回值是boolean /*System.out.println("方法2 耗时:" + (System.currentTimeMillis() - start) + " 毫秒");*/ return list1; } // 方法3,用Map存放List1和List2的元素作为key,value为其在List1和List2中出现的次数 // 出现次数为1的即为不同元素,查找次数为list1.size() + list2.size(),较方法1和2,是极大简化 private static List getDiffrentThree(List list1, List list2) { List diff = new ArrayList (); /*long start = System.currentTimeMillis();*/ Map map = new HashMap (list1.size() + list2.size()); // 将List1元素放入Map,计数1 for (String string : list1) { map.put(string, 1); } // 遍历List2,在Map中查找List2的元素,找到则计数+1;未找到则放入map,计数1 for (String string : list2) { Integer count = map.get(string); if (count != null) { map.put(string, ++count);// 此处可优化,减少put次数,即为方法4 continue; } map.put(string, 1); } for (Map.Entry entry : map.entrySet()) { if (entry.getValue() == 1) { diff.add(entry.getKey()); } } /*System.out.println("方法3 耗时:" + (System.currentTimeMillis() - start) + " 毫秒");*/ return diff; } // 优化方法3,减少put次数 private static List getDiffrentFour(List list1, List list2) { List diff = new ArrayList (); /* long start = System.currentTimeMillis();*/ Map map = new HashMap (list1.size() + list2.size()); List maxList = list1; List minList = list2; if (list2.size() > list1.size()) { maxList = list2; minList = list1; } for (String string : maxList) { map.put(string, 1); } for (String string : minList) { Integer count = map.get(string); if (count != null) { map.put(string, ++count); continue; } map.put(string, 1); } for (Map.Entry entry : map.entrySet()) { if (entry.getValue() == 1) { diff.add(entry.getKey()); } } /*System.out.println("方法3 耗时:" + (System.currentTimeMillis() - start) + " 毫秒");*/ return diff; }}
第二种使用com.google.common.collect的工具类
com.google.guava guava 20.0
ListlistA = new ArrayList (); List listB = new ArrayList (); for (SessionIdB sessionIdB : sessionIdBll) { listA.add(sessionIdB .getSessionIdB()); } for (SessionIdA sessionIdA : sessionIdAll) { listB.add(sessionIdA .getSessionIdA()); } HashSet setA = new HashSet (listA); HashSet setB = new HashSet (listB); Sets.SetView difference = Sets.difference(setA, setB); List listC=new ArrayList (); for (String string : difference) { logger.info("-----获取的sessionId--------"+string); listC.add(string); }
《参考代码如下》
set的交集, 并集, 差集HashSet setA = newHashSet(1, 2, 3, 4, 5); HashSet setB = newHashSet(4, 5, 6, 7, 8); SetView union = Sets.union(setA, setB); System.out.println("union:"); for (Integer integer : union) System.out.println(integer); //union:12345867 SetView difference = Sets.difference(setA, setB); System.out.println("difference:"); for (Integer integer : difference) System.out.println(integer); //difference:123 SetView intersection = Sets.intersection(setA, setB); System.out.println("intersection:"); for (Integer integer : intersection) System.out.println(integer); //intersection:45
第三种检查参数是否有值或者为空
方法一(数据量大,效率低):if(list!=null && list.size()>0){}方法二(数据量大,效率高):if(list!=null && !list.isEmpty()){}//use javaif(list!=null && list.size()>0)'''if(str!=null && str.length()>0)'''if(str !=null && !str.isEmpty())//use guavaif(!Strings.isNullOrEmpty(str))//use javaif (count <= 0) { throw new IllegalArgumentException("must be positive: " + count); } //use guavaPreconditions.checkArgument(count > 0, "must be positive: %s", count);
转载地址:http://aoipn.baihongyu.com/