//java实现求交集,并集,包括元素为对象和基本类型,主要是利用hashMap,set不允许元素重复等特性来进行实现去重,利用反射机制来灵活配置以对象某个属性来进行去重。
/**
* Gaoxl
* 求并集去重
* 基本类型和对象
* @param list1
* @param list2
* @param fieldName(用于去重的对象属性名)
* @return
*/
public static List getUnion(List list1,List list2,String fieldName) {
list1.addAll(list2);
if(null != fieldName){//对象
Map map = new HashMap();
List list = new ArrayList();
try{
PropertyDescriptor propertyDescription = new PropertyDescriptor(fieldName, list1.get(0).getClass());
Method readMethod = propertyDescription.getReadMethod();
for(T li : list1){//根据某属性值去重
Object fieldValue = readMethod.invoke(li);
map.put(fieldValue, li);
}
}catch(Exception e){
e.printStackTrace();
return null;
}
for(Object key : map.keySet()){
list.add(map.get(key));
}
return list;
}else{//基本类型
Set set = new HashSet<>(list1);
return new ArrayList(set);
}
}
/**
* Gaoxl
* 求交集并去重
* 基本类型和对象
* @param list1
* @param list2
* @param fieldName(用于去重的对象属性名)
* @return
*/
public static List getIntersection(List list1,List list2,String fieldName){
List result = new ArrayList();
if(null != fieldName){
try{
Map map = new HashMap();
PropertyDescriptor propertyDescriptor = new PropertyDescriptor(fieldName, list1.get(0).getClass());
Method readMethod = propertyDescriptor.getReadMethod();
Map temp = new HashMap();
for(T li : list1){
map.put(readMethod.invoke(li), li);
}
for(T li : list2){
Object key = readMethod.invoke(li);
if(map.containsKey(key)){
temp.put(key, li);
}
}
for(Object key : temp.keySet()){
result.add(temp.get(key));
}
}catch(Exception e){
e.printStackTrace();
return null;
}
}else{
Set set1 = new HashSet<>(list1);
Set set2 = new HashSet<>(list2);
for(T li : set2){
if(set1.contains(li)){//求交集
result.add(li);
}
}
}
return result;
}
//User类
public class User{
private Long userId;
private String userName;
public Long getUserId() {
return userId;
}
public void setUserId(Long userId) {
this.userId = userId;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
}
//测试类
public class testMethod {
public static void main(String[] args){
List list = new ArrayList<>();
list.add("1");
list.add("2");
list.add("2");
List list2 = new ArrayList<>();
list2.add("2");
list2.add("4");
list2.add("5");
//List listResult = SysUtil.getUnion(list, list2, null);
//System.out.println(listResult);
List list3 = new ArrayList<>();
User user = new User();
user.setUserId(2L);
user.setUserName("admin");
list3.add(user);
User user1 = new User();
user1.setUserId(2L);
user1.setUserName("admin2");
list3.add(user1);
User user2 = new User();
user2.setUserId(3L);
user2.setUserName("admin3");
list3.add(user2);
List list4 = new ArrayList<>();
User user3 = new User();
user3.setUserId(4L);
user3.setUserName("admin2");
list4.add(user3);
User user4 = new User();
user4.setUserId(6L);
user4.setUserName("admin4");
list4.add(user4);
User user5 = new User();
user5.setUserId(5L);
user5.setUserName("admin5");
list4.add(user5);
//List list5 = SysUtil.getUnion(list3, list4, "userId");
//System.out.println(list5);
List list6 = SysUtil.getIntersection(list, list2, null);
System.out.println(list6);
List list7 = SysUtil.getIntersection(list3, list4, "userId");
System.out.println(list7);
}
}