TreeMap排序的使用


    /**
     * @param  userIdList  用戶id的集合
     * @throws ActiveRecordException 
     * @return  返回一個最少的任務數的用戶ID
     */
    public String  getLeastTask( List<Record> userIdList) throws ActiveRecordException {
        String     userId =null;
        // 定義一個Map 
        Map<String ,Integer>  map = Maps.newTreeMap();
        // 獲取用戶的ID   便利
        for (int i = 0; i < userIdList.size(); i++) {
            userId=userIdList.get(i).getStr("userId");
            // 查詢里面最少任務的人
            Record UserTaskCount=this.getUserTaskCountList(userId);
            // 獲取用戶的id
            String user= UserTaskCount.getStr("userId");
            //獲取用戶的任務的計數
            int taskCount=UserTaskCount.getLong("num").intValue();
            map.put(user,taskCount);
        }
         //將Map轉為List ,進行排序
        List<Map.Entry<String, Integer>> list = new ArrayList<>(map.entrySet());
        Collections.sort(list,((o1, o2) -> o2.getValue().compareTo(o1.getValue())));
        // 最少人員的userId
        String mixUserId=list.get(list.size()-1).getKey();
        return mixUserId;
        
    }
//測試
    public static void main(String[] args) {
        
            Map<String,Integer> map = Maps.newTreeMap();
            map.put("sssss",-111111111);
            map.put("ss",-111111111);
            map.put("yy",0);
            map.put("lx",5);
            map.put("fyx",2);
            map.put("ztt",3);
            map.put("zzy",10);
            List<Map.Entry<String,Integer>> list = new ArrayList<>(map.entrySet());

            Collections.sort(list,((o1, o2) -> o2.getValue().compareTo(o1.getValue())));

            System.out.println(list.get(list.size()-1).getValue());

            System.out.println(list.get(list.size()-1).getKey());


        
    }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM