import java.util.*; public class CollectionUtils { /** * 找出两个集合中不同的元素 * * @param collmax * @param collmin * @return */ public static Collection getDifferentLeft(Collection collmax, Collection collmin) { //使用LinkedList防止差异过大时,元素拷贝 Collection csReturn = new LinkedList(); Collection max = collmax; Collection min = collmin; //先比较大小,这样会减少后续map的if判断次数 if (collmax.size() < collmin.size()) { max = collmin; min = collmax; } //直接指定大小,防止再散列 Map<Object, Integer> map = new HashMap<>(max.size()); for (Object object : max) { map.put(object, 1); } for (Object object : min) { if (map.get(object) == null) { //csReturn.add(object); } else { map.put(object, 2); } } for (Map.Entry<Object, Integer> entry : map.entrySet()) { if (entry.getValue() == 1) { csReturn.add(entry.getKey()); } } return csReturn; } /** * 找出两个集合中不同的元素 * * @param collmax * @param collmin * @return */ public static Collection getDifferentRight(Collection collmax, Collection collmin) { //使用LinkedList防止差异过大时,元素拷贝 Collection csReturn = new LinkedList(); Collection max = collmax; Collection min = collmin; //先比较大小,这样会减少后续map的if判断次数 if (collmax.size() < collmin.size()) { max = collmin; min = collmax; } //直接指定大小,防止再散列 Map<Object, Integer> map = new HashMap<>(max.size()); for (Object object : max) { map.put(object, 1); } for (Object object : min) { if (map.get(object) == null) { csReturn.add(object); } else { map.put(object, 2); } } for (Map.Entry<Object, Integer> entry : map.entrySet()) { if (entry.getValue() == 1) { //csReturn.add(entry.getKey()); } } return csReturn; } /** * 找出两个集合中不同的元素 * * @param collmax * @param collmin * @return */ public static Collection getDifferentAll(Collection collmax, Collection collmin) { //使用LinkedList防止差异过大时,元素拷贝 Collection csReturn = new LinkedList(); Collection max = collmax; Collection min = collmin; //先比较大小,这样会减少后续map的if判断次数 if (collmax.size() < collmin.size()) { max = collmin; min = collmax; } //直接指定大小,防止再散列 Map<Object, Integer> map = new HashMap<>(max.size()); for (Object object : max) { map.put(object, 1); } for (Object object : min) { if (map.get(object) == null) { csReturn.add(object); } else { map.put(object, 2); } } for (Map.Entry<Object, Integer> entry : map.entrySet()) { if (entry.getValue() == 1) { csReturn.add(entry.getKey()); } } return csReturn; } /** * 找出两个集合中相同的元素 * * @param collmax * @param collmin * @return */ public static Collection getSame(Collection collmax, Collection collmin) { //使用LinkedList防止差异过大时,元素拷贝 Collection csReturn = new LinkedList(); Collection max = collmax; Collection min = collmin; //先比较大小,这样会减少后续map的if判断次数 if (collmax.size() < collmin.size()) { max = collmin; min = collmax; } //直接指定大小,防止再散列 Map<Object, Integer> map = new HashMap<>(max.size()); for (Object object : max) { map.put(object, 1); } for (Object object : min) { if (map.get(object) != null) { csReturn.add(object); } } return csReturn; } //main public static void main(String[] args) { List<String> list1 = new ArrayList<>(); list1.add("1"); list1.add("2"); list1.add("3"); list1.add("4"); List<String> list2 = new ArrayList<>(); list2.add("12"); list2.add("2"); list2.add("4"); list2.add("23"); List<String> diffrent5 = getDiffrentList1(list1, list2); System.out.println(diffrent5); Collection different = CollectionUtils.getDifferentLeft(list1, list2); Collection different2 = CollectionUtils.getSame(list1, list2); System.out.println(different); System.out.println(different2); }