java实现交集,并集,包括对象和基本类型

//java实现求交集,并集,包括元素为对象和基本类型,主要是利用hashMap,set不允许元素重复等特性来进行实现去重,利用反射机制来灵活配置以对象某个属性来进行去重。
/** * Gaoxl * 求并集去重 * 基本类型和对象 * @param list1 * @param list2 * @param fieldName(用于去重的对象属性名) * @return */ public static <T> List<T> getUnion(List<T> list1,List<T> list2,String fieldName) { list1.addAll(list2); if(null != fieldName){//对象 Map<Object,T> map = new HashMap<Object, T>(); List<T> list = new ArrayList<T>(); 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<T> set = new HashSet<>(list1); return new ArrayList<T>(set); } } /** * Gaoxl * 求交集并去重 * 基本类型和对象 * @param list1 * @param list2 * @param fieldName(用于去重的对象属性名) * @return */ public static <T> List<T> getIntersection(List<T> list1,List<T> list2,String fieldName){ List<T> result = new ArrayList<T>(); if(null != fieldName){ try{ Map<Object,T> map = new HashMap<Object, T>(); PropertyDescriptor propertyDescriptor = new PropertyDescriptor(fieldName, list1.get(0).getClass()); Method readMethod = propertyDescriptor.getReadMethod(); Map<Object,T> temp = new HashMap<Object, T>(); 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<T> set1 = new HashSet<>(list1); Set<T> 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<String> list = new ArrayList<>();
		list.add("1");
		list.add("2");
		list.add("2");
		List<String> list2 = new ArrayList<>();
		list2.add("2");
		list2.add("4");
		list2.add("5");
		//List<String> listResult = SysUtil.getUnion(list, list2, null);
		//System.out.println(listResult);
		
		List<User> 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<User> 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<User> list5 = SysUtil.getUnion(list3, list4, "userId");
		//System.out.println(list5);
		
		List<String> list6 = SysUtil.getIntersection(list, list2, null);
		System.out.println(list6);
		
		List<User> list7 = SysUtil.getIntersection(list3, list4, "userId");
		System.out.println(list7);
	}
}

  


  

posted @ 2019-11-27 14:10  Carol淋  阅读(1006)  评论(0编辑  收藏  举报