/** * @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()); }