java通过属性对集合进行分组(可多个对象)

1.首先我先建一个实体类

public class Bill {

	private String type;
	private String state;
	private Integer money;
	public String getType() {
		return type;
	}
	public void setType(String type) {
		this.type = type;
	}
	public String getState() {
		return state;
	}
	public void setState(String state) {
		this.state = state;
	}
	public Integer getMoney() {
		return money;
	}
	public void setMoney(Integer money) {
		this.money = money;
	}
	@Override
	public String toString() {
		return "Bill [type=" + type + ", state=" + state + ", money=" + money + "]";
	}

2.接下来就是方法

public class MergeList1 {
	
	public static void main(String[] args) {
		List list = new ArrayList();  
        Bill b = new Bill();  
        b.setType("A");  
        b.setMoney(1);
        b.setState("中");
        list.add(b);  
        b = new Bill();  
        b.setType("B");  
        b.setState("大");
        b.setMoney(2);  
        list.add(b);  
        b = new Bill();  
        b.setType("C");  
        b.setMoney(3);  
        b.setState("大");
        list.add(b);  
        b = new Bill();  
        b.setType("A");  
        b.setMoney(2);
        b.setState("大");
        list.add(b);  
        b = new Bill();  
        b.setType("B");  
        b.setMoney(2);  
        b.setState("中");
        list.add(b);  
        b = new Bill();  
        b.setType("C");  
        b.setMoney(3);  
        b.setState("大");
        list.add(b);  
        
        b = new Bill();  
        b.setType("A");  
        b.setMoney(1);  
        b.setState("中");
        list.add(b);  
        b = new Bill();  
        b.setType("B"); 
        b.setState("中");
        b.setMoney(2);  
        list.add(b);  
        b = new Bill();  
        b.setType("C");  
        b.setState("大");
        b.setMoney(3);  
        list.add(b);  
        b = new Bill();  
        b.setType("C");  
        b.setState("大");
        b.setMoney(1);  
        list.add(b);  
        
        for (Bill bill : list) {
			System.out.println(bill);
		}
  
        List bi = new ArrayList();  
        System.out.println("==================");
        
        for (Bill bil : bi) {
			System.out.println(bil);
		}
        
        for (Bill bill : list) {  
            boolean flag = false;
            String a1 = bill.getType()+","+bill.getState();
			System.out.println("a1="+a1);
            for (Bill bills : bi) {
            	String a2 = bills.getType()+","+bills.getState();
    			System.out.println("a2="+a2);
                if(a1.equals(a2)){  
                    int money = bills.getMoney();  
                    money += bill.getMoney();  
                    bills.setMoney(money);  
                    flag = true;  
                }  
            }
            if(!flag){  
                bi.add(bill);  
            }  
        }  
        
        for (Bill bill : bi) {  
            System.out.println(bill.getType() +"    " +bill.getState()+"    "+bill.getMoney());  
        }
	}
}

 

你可能感兴趣的:(生活学习)