1. 程式人生 > >TreeMap Comparator 根據vlaue 值排序案例

TreeMap Comparator 根據vlaue 值排序案例

測試類:



public class TestMapSort {
public static void main(String[] args) {  
 
        HashMap<String, LifeDataModel> map = new HashMap<String, LifeDataModel>();  
        ValueComparator bvc =  new ValueComparator(map);  
        TreeMap<String, LifeDataModel> sorted_map = new TreeMap<String, LifeDataModel>(bvc);  
        LifeDataModel model1 = new LifeDataModel();
      
        
        model1.setCallNum(100);
        model1.setCallTime(255);
        model1.setHighCallNum(10);
        
        LifeDataModel model2 = new LifeDataModel();
        model2.setCallNum(90);
        model2.setCallTime(255);
        model2.setHighCallNum(10);
        
        LifeDataModel model3 = new LifeDataModel();
        model3.setCallNum(200);
        model3.setCallTime(255);
        model3.setHighCallNum(10);
        
        map.put("10001",model1);  
        map.put("10002",model2); 
        map.put("10003",model3); 
       
  
        System.out.println("unsorted map: "+map);  
  
        sorted_map.putAll(map);  
  
        System.out.println("results: "+sorted_map);  
    }  
}


class ValueComparator implements Comparator<String> {
Map<String, LifeDataModel> baseMap ;
public ValueComparator(Map<String, LifeDataModel> baseMap) {  
       this.baseMap = baseMap;  
}  

@Override
public int compare(String a, String b) {  
        if (baseMap.get(a).getCallNum() >= baseMap.get(b).getCallNum()) {  
            return -1;  
        } else {  
            return 1;  
        } 
    }  
}

l類物件:

public class LifeDataModel {


/*private boolean newStatus = false;
private boolean oldStatus = false;
private double newPremiun = 0.0;
private double oldPremiun = 0.0;
private int custNum = 0;*/
//以下3個指標都是一個前後變化的差值 
private int callNum;//通次
private double callTime;//通時
private int highCallNum;//通時在1000s以上的通次
public int getCallNum() {
return callNum;
}

public void setCallNum(int callNum) {
this.callNum = callNum;
}
public double getCallTime() {
return callTime;
}
public void setCallTime(double callTime) {
this.callTime = callTime;
}
public int getHighCallNum() {
return highCallNum;
}
public void setHighCallNum(int highCallNum) {
this.highCallNum = highCallNum;
}

private int submittedNum;//線上提交件
private double submittedPremium;//線上提交保費

private int confirmedNum;//線上成交件
private double confirmedNumPremium;//線上成交保費
public int getSubmittedNum() {
return submittedNum;
}


public void setSubmittedNum(int submittedNum) {
this.submittedNum = submittedNum;
}


public double getSubmittedPremium() {
return submittedPremium;
}


public void setSubmittedPremium(double submittedPremium) {
this.submittedPremium = submittedPremium;
}


public int getConfirmedNum() {
return confirmedNum;
}


public void setConfirmedNum(int confirmedNum) {
this.confirmedNum = confirmedNum;
}


public double getConfirmedNumPremium() {
return confirmedNumPremium;
}


public void setConfirmedNumPremium(double confirmedNumPremium) {
this.confirmedNumPremium = confirmedNumPremium;
}



}