java合并查询的List中的属性

Posted on 2013-04-18 13:45  IT_louis  阅读(464)  评论(0编辑  收藏  举报
package com;
/**
 * 
 * @author Liu li
 *
 */
public class Bean {
  private String id;
  private String name;
  private String age;
public String getId() {
 return id;
}
public void setId(String id) {
 this.id = id;
}
public String getName() {
 return name;
}
public void setName(String name) {
 this.name = name;
}
public String getAge() {
 return age;
}
public void setAge(String age) {
 this.age = age;
}
  
}



package com;
/**
 * 
 * @author Liu li
 *
 */
public class Tbean {
  private String id;
  private String name;
public String getId() {
 return id;
}
public void setId(String id) {
 this.id = id;
}
public String getName() {
 return name;
}
public void setName(String name) {
 this.name = name;
}
}

 

package com;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * 
 * @author Liu li
 *
 */

public class Main {

 /**
  * @param args
  */
 public static void main(String[] args) {
  
      new Main().test();
 }
  public void test(){
    List<Bean> templist=new ArrayList<Bean>();
    Bean b1=new Bean();
    b1.setId("001");
    b1.setAge("001");
    b1.setName("name001");
   templist.add(b1);
   
    Bean b2=new Bean();
    b2.setId("001");
    b2.setAge("001");
    b2.setName("name002");
    templist.add(b2);
    
    Bean b3=new Bean();
    b3.setId("001");
    b3.setAge("001");
    b3.setName("name003");
    templist.add(b3);
    
    Bean b4=new Bean();
    b4.setId("004");
    b4.setAge("004");
    b4.setName("name004");
    templist.add(b4);
   //打印出没转换之前的数据样式
    for(Bean tt:templist){
     System.out.println(tt.getId()+","+tt.getName());
    }
    System.out.println("---------");
    //将重复的条数的属性合并到同一属性下,要合并的属性的记录全部相同
    for(Bean b:templist){
   for(Bean tb:templist){
    if(b.getId().equals(tb.getId())){
    b.setName(b.getName()+","+tb.getName());
    for(Bean tt:templist){
        
        Set<String> tpset=new HashSet<String>();
      String[] lin=tt.getName().split(",");
      for(int i=0;i<lin.length;i++){
       tpset.add(lin[i]);
      }
      String temps="";
      for(String str:tpset){
       temps+=","+str;
      }
      tt.setName(temps);
       }
    }
   } 
    }
    for(Bean tt:templist){
     System.out.println(tt.getId()+","+tt.getName());
    }
    System.out.println("---------");
    //将相同属性中的相同项去除
//    for(Bean tt:templist){
//     
//     Set<String> tpset=new HashSet<String>();
//   String[] lin=tt.getName().split(",");
//   for(int i=0;i<lin.length;i++){
//    tpset.add(lin[i]);
//   }
//   String temps="";
//   for(String str:tpset){
//    temps+=","+str;
//   }
//   tt.setName(temps);
//    }
//    for(Bean tt:templist){
//     System.out.println(tt.getId()+","+tt.getName());
//    }
//    System.out.println("---------");
   
    for(int i=0;i<templist.size()-1;i ++ ){ 
      for(int j=templist.size()-1;j>i;j--){ 
        if(templist.get(j).getId().equals(templist.get(i).getId()))   { 
         templist.remove(j); 
        } 
      } 
    } 
    for(Bean tt:templist){
     System.out.println(tt.getId()+","+tt.getName());
    }
    System.out.println("---------");
  
    
    
   }
 
}
结果的打印
-----
001,name001
001,name002
001,name003
004,name004
---------
001,,name002,,name001,name003
001,,name002,,name001,name003
001,,name002,,name001,name003
004,,,name004
---------
001,,name002,,name001,name003
004,,,name004
---------

 

Copyright © 2024 IT_louis
Powered by .NET 9.0 on Kubernetes