将重复的数据合并成一行,希望能做个参考
package com.jy.common; import com.jy.model.OrderHandling; import java.beans.IntrospectionException; import java.beans.PropertyDescriptor; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.util.*; public class DistinctUtils { //传入一个集合,将里面的数据根据某个字段判断重复,将一个或者多个值合并 /** * * @param objLIst 传入一个集合,只对对象有效 * @param selectNumber 去重字段 * @param merge 将参数合并 * @return 过滤后的集合 */ public static List<?> DistinctUtils(List<?> objLIst,String selectNumber,String merge ) throws ClassNotFoundException, NoSuchMethodException, IllegalAccessException, InstantiationException, InvocationTargetException, IntrospectionException { WeakHashMap map=new WeakHashMap(); ArrayList list = new ArrayList(); selectNumber="get"+selectNumber; String changeMerge="get"+merge; for(int i=0;i<objLIst.size();i++){ Object newClass= objLIst.get(i);//获取class Method method = newClass.getClass().getMethod(selectNumber);//获取名称OrderNumber String OrderNumber =(String)method.invoke(newClass);//执行方法 Method mergemethod = newClass.getClass().getMethod(changeMerge);//获取名称CargoName String CargoName = (String)mergemethod.invoke(newClass);//执行方法 if(map.containsKey(OrderNumber)){//有了 //CargoName StringBuffer sb=new StringBuffer(); //获取的是上一个对象,所以还是要创建 Object keynewClass = map.get(OrderNumber); //获取名称OrderNumber Method keymergemethod = keynewClass.getClass().getMethod(changeMerge); //执行方法 String keyCargoName =(String)keymergemethod.invoke(keynewClass); //上一个值,或者初始值 sb.append(keyCargoName); //当前值 sb.append(","+CargoName); //拼装好的数据 String value = sb.toString(); //调用set方法 名称,对象class,修改后 PropertyDescriptor pd = new PropertyDescriptor(merge,newClass.getClass()); Method setMethod = pd.getWriteMethod(); //拿到当前循环对象。修改set merge方法 setMethod.invoke(newClass, value); //map返回 map.put(OrderNumber,newClass); }else{ //没有 map.put(OrderNumber,newClass); } } //循环对象map for (Object v : map.values()) { list.add(v); } map=null; return list; } public static void main(String[] args) throws ClassNotFoundException, NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException, IntrospectionException { List<OrderHandling> orderHandlingss =new ArrayList<OrderHandling>(); int size = orderHandlingss.size(); OrderHandling or=new OrderHandling(); or.setOrd_number("001"); or.setCargoName("测试001"); orderHandlingss.add(or); OrderHandling or2=new OrderHandling(); or2.setOrd_number("001"); or2.setCargoName("测试002"); orderHandlingss.add(or2); OrderHandling or3=new OrderHandling(); or3.setOrd_number("001"); or3.setCargoName("测试003"); orderHandlingss.add(or3); OrderHandling or33=new OrderHandling(); or33.setOrd_number("002"); or33.setCargoName("yang"); orderHandlingss.add(or33); OrderHandling or23=new OrderHandling(); or23.setOrd_number("001"); or23.setCargoName("测试0023"); orderHandlingss.add(or23); List<OrderHandling> objects = (List<OrderHandling>) DistinctUtils.DistinctUtils(orderHandlingss, "Ord_number", "CargoName"); System.out.println(objects.toString()); } }