package com.wst.testFor;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
/**
* 该类提供对集合类的高效操作
*
* @author Czp
*
*/
public class CollectionUtil {
public static void main(String[] args) {
String[] arr01={"Andy","Bill","Cindy","Douglas","Felex","Green"};
String[] arr02={"Andy","Bill","Felex","Green","Gates","daf"};
ArrayList list1 = new ArrayList<String>();
ArrayList list2 = new ArrayList<String>();
list1.add("1");
list1.add("2");
list1.add("3");
list2.add("1");
list2.add("2");
list2.add("4");
List<String> list = new ArrayList<String>(arr01.length);
Collections.addAll(list, arr01);
System.out.println(CollectionUtil.getDiffentNoDuplicate(list1,list));
}
/**
* 不允许实例化
*/
private CollectionUtil() {
}
/**
* 获取两个集合的不同元素
*
* @param collmax
* @param collmin
* @return
*/
@SuppressWarnings({ "rawtypes", "unchecked" })
public static Collection getDiffent(Collection collmax, Collection collmin) {
// 使用LinkeList防止差异过大时,元素拷贝
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<Object, Integer>(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
*/
@SuppressWarnings({ "rawtypes", "unchecked" })
public static Collection getDiffentNoDuplicate(Collection collmax, Collection collmin) {
return new HashSet(getDiffent(collmax, collmin));
}
}
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
/**
* 该类提供对集合类的高效操作
*
* @author Czp
*
*/
public class CollectionUtil {
public static void main(String[] args) {
String[] arr01={"Andy","Bill","Cindy","Douglas","Felex","Green"};
String[] arr02={"Andy","Bill","Felex","Green","Gates","daf"};
ArrayList list1 = new ArrayList<String>();
ArrayList list2 = new ArrayList<String>();
list1.add("1");
list1.add("2");
list1.add("3");
list2.add("1");
list2.add("2");
list2.add("4");
List<String> list = new ArrayList<String>(arr01.length);
Collections.addAll(list, arr01);
System.out.println(CollectionUtil.getDiffentNoDuplicate(list1,list));
}
/**
* 不允许实例化
*/
private CollectionUtil() {
}
/**
* 获取两个集合的不同元素
*
* @param collmax
* @param collmin
* @return
*/
@SuppressWarnings({ "rawtypes", "unchecked" })
public static Collection getDiffent(Collection collmax, Collection collmin) {
// 使用LinkeList防止差异过大时,元素拷贝
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<Object, Integer>(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
*/
@SuppressWarnings({ "rawtypes", "unchecked" })
public static Collection getDiffentNoDuplicate(Collection collmax, Collection collmin) {
return new HashSet(getDiffent(collmax, collmin));
}
}