例如 :

List<obj1> obj1List ,List<obj2> obj2List,需要按照obj1.id=obj2.id进行整合
select * from obj1List left join obj2List on obj1.id=obj2.id

第一步:将obj1List转换成map

将list转成id为key,obj1为value的map对象

  public static <K, V> Map<K, V> listToMap(List<V> list, String keyMethodName, Class<V> c) {
      Map<K, V> map = new HashMap<K, V>();
      if (list != null) {
          try {
              Method methodGetKey = c.getMethod(keyMethodName);
              for (int i = 0; i < list.size(); i++) {
                  V value = list.get(i);
                  @SuppressWarnings("unchecked")
                  K key = (K) methodGetKey.invoke(list.get(i));
                  map.put(key, value);
              }
          } catch (Exception e) {
              throw new IllegalArgumentException("field can't match the key!");
          }
      }

      return map;
  }

第二步:根据key值将obj2List进行copy

for (Obj2 obj: obj2List) {

      try {
          System.out.println(obj);
          BeanUtils.copyProperties(mapFromList1.get(obj.getId()), obj);
      } catch (Exception e) {
          e.printStackTrace();
      }
  }

另一个例子:
java代码

public class TEST {
    public static void main(String[] args) {
       
        List<Model1> list1 = new ArrayList();
        List<Model1> list2 = new ArrayList();
        List<Model2> list3= new ArrayList();
        for (int i=0;i<list1.size();i++){
            for (int j=0;j< list2.size();j++){
//判断两个集合no是否相等
               if (list1.get(i).getNo()==list2.get(i).getNo()){
//给第三个集合赋值并计算different
                   Model2 model2=new Model2();
                   model2.setName(list1.get(i).getName());
                   model2.setNo(list1.get(i).getNo());
                   model2.setDifferent(list1.get(i).getName()-list2.get(i).getName());
               list3.add(model2);
               }
            }
        }
        System.out.println(list3);
    }
    
    private static class Model2 {
        Integer no;
        Integer name;
        Integer different;
    
        public Integer getNo() {
            return no;
        }
    
        public void setNo(Integer no) {
            this.no = no;
        }
    
        public Integer getName() {
            return name;
        }
    
        public void setName(Integer name) {
            this.name = name;
        }
    
        public Integer getDifferent() {
            return different;
        }
    
        public void setDifferent(Integer different) {
            this.different = different;
        }
    }
    private static class Model1 {
        Integer no;
        Integer name;
    
        public Integer getNo() {
            return no;
        }
    
        public void setNo(Integer no) {
            this.no = no;
        }
    
        public Integer getName() {
            return name;
        }
    
        public void setName(Integer name) {
            this.name = name;
        }
    }
}

参考文章:https://ask.csdn.net/questions/7715948

标签: none

相关阅读

  • 测试信息
  • 开发商:阿里巴巴
  • 版本号:1.0
  • 配色:(企业家)” 推选活动结果
  • 测试信息
  • 测试信息
  • 测试信息