关于ArrayList中对象按照属性进行分类(属性相同的对象放入新的arrayList里面)

关于ArrayList中对象按照属性进行分类(属性相同的对象放入新的arrayList里面)

package com.zotn.test;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Map;
import java.util.TreeMap;
public class ArrayListType {
 public Map<String,ArrayList>  sort(ArrayList list){
        TreeMap tm=new TreeMap();
        for(int i=0;i<list.size();i++){
         SupplierBean s=(SupplierBean)list.get(i);
            if(tm.containsKey(s.getBukrs())){//
             ArrayList l11=(ArrayList)tm.get(s.getBukrs());
             l11.add(s);
            }else{
             ArrayList tem=new ArrayList();
                tem.add(s);
                tm.put(s.getBukrs(), tem);
            }
            
        }
        return tm;
    }
    public static void main(String[] args) throws IOException{
        ArrayList list=new ArrayList();
        SupplierBean s1=new SupplierBean();
        s1.setBukrs("1000");
        s1.setName1("总1");
        
        SupplierBean s2=new SupplierBean();
        s2.setBukrs("1000");
        s2.setName1("总2");
        SupplierBean s3=new SupplierBean();
        s3.setBukrs("1100");
        s3.setName1("分1");
        SupplierBean s4=new SupplierBean();
        s4.setBukrs("1100");
        s4.setName1("分2");
        
        SupplierBean s5=new SupplierBean();
        s5.setBukrs("4300");
        s5.setName1("山东1");
        list.add(s1);
        list.add(s2);
        list.add(s3);
        list.add(s4);
        list.add(s5);
        
        Map<String,ArrayList> ss =new ArrayListType().sort(list);
        Iterator it = ss.keySet().iterator();
        while(it.hasNext()){
         String key = (String)it.next();
         System.out.print("\n"+key+":");
         ArrayList list1 = ss.get(key);
         for(int i=0; i<list1.size(); i++){
          SupplierBean sb = (SupplierBean)list1.get(i);
          System.out.print("   "+sb.getName1());
         }
         
        }
    }
}


 

工具类:

package com.zotn.test; 

public class SupplierBean {
 

 private String bukrs;

 private String lifnr;

 private String name1;

 private String bkzkp;
 public String getBukrs() {
  return bukrs;
 }
 public void setBukrs(String bukrs) {
  this.bukrs = bukrs;
 }
 public String getLifnr() {
  return lifnr;
 }
 public void setLifnr(String lifnr) {
  this.lifnr = lifnr;
 }
 public String getName1() {
  return name1;
 }
 public void setName1(String name1) {
  this.name1 = name1;
 }
 public String getBkzkp() {
  return bkzkp;
 }
 public void setBkzkp(String bkzkp) {
  this.bkzkp = bkzkp;
 }
}


打印结果:

1000:   总1   总2
1100:   分1   分2
4300:   山东1

你可能感兴趣的:(关于ArrayList中对象按照属性进行分类(属性相同的对象放入新的arrayList里面))