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;
}



}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值