获取两个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-10-31 17:13:56