目的:将一个java Map("String","ActionStatResult")根据其value中ActionStatResult的字段来排序,并得到该对象的倒序?? List<ActionStatResult>;
思路:1. 用map.values()分离出Map中的value值对象,并转化为List<ActionStatResult>
???????? 2. 对List<ActionStatResult>进行排序,可参考Collections.sort(List,Comparator)排序;
参考代码如下:
public List<ActionStatResult> mapArrayListSort(Map<String,ActionStatResult> map ){
????? //将Map中的value(其中放着ActionStatResult)值提取出来并转化为一个List对象
????? List<ActionStatResult> actionList = new ArrayList<ActionStatResult>(map.values());
????? // sort排序, 其中用匿名内部类比较器..
????? Collections.sort(actionList,new Comparator<ActionStatResult>(){
???????? @Override
???????? public int compare(ActionStatResult o1, ActionStatResult o2) { //倒序排列
???????????? int flag = Long.valueOf(o2.getMaxExecuteTime()).compareTo(Long.valueOf(o1.getMaxExecuteTime()));
????????????? if (flag == 0) {
????????????????? return Long.valueOf(o2.getExecuteCount()).compareTo(Long.valueOf(o1.getExecuteCount()));
????????????? }else{
???????????????????? return flag;
????????????????? }
????????????? }
??? });
???? return actionList;
?}
//例:ActionStatResult对象
public class ActionStatResult {
?? private String actionClassName;????????? //action类名
?? private String actionMethodName;????? //action 方法名
?? private double avgExecuteTime;???????? // 执行平均时长(毫秒)
?? private long maxExecuteTime = Long.MIN_VALUE;?? // 执行最长时长(毫秒)
?? private long minExecuteTime = Long.MAX_VALUE;?? // 执行最短时长(毫秒)
?? private long executeCount;????????? // 执行次数
?? //get- and set- ....
}
在这里与大家分享一下本人在实际开发过程有用的小模块,有更好的做法或有什么想法,可以大家进行交流。。。
?
?
?
?
?
?