HashMap按照value值进行排序

来源:互联网 发布:怎么缴费过期域名 编辑:程序博客网 时间:2024/05/20 18:41
今天的工作中需要对HashMap中的value值进行从大到小排序,并取出前6个值,持久化到properties文件中,代码如下
Map<Camera,Long> commonCamera = new HashMap<Camera,Long>();tt = new TimerTask(){@Overridepublic void run() {try {Properties prop = new Properties();OutputStream out = new FileOutputStream("commonUse.properties");List<Map.Entry<Camera, Long>> list = new ArrayList<Map.Entry<Camera,Long>>(commonCamera.entrySet());Collections.sort(list, new Comparator<Map.Entry<Camera, Long>>(){@Overridepublic int compare(Entry<Camera, Long> o1,Entry<Camera, Long> o2) {//按从大到小排序return (int) (o2.getValue() - o1.getValue());}});for(int i = 0; i <(list.size()>12?12:list.size()); i++){prop.setProperty(list.get(i).getKey().getDepartment().getNo()+"", list.get(i).getValue().toString());}prop.store(out, "");logger.info("6小时重复任务:向commonUse.properties文件写入‘常用摄像头’信息");} catch (Exception e) {e.printStackTrace();}}};timer.schedule(tt, 0, 1000*60*60*6);

;

1 0
原创粉丝点击