等值排名算法,找出一个数字在该集合中排第几,并按照输入顺序,输出排序值
/** * 等值排名算法 * * @author chenmin */ public class RankUtil { //输入 3,9 ,10 ,4
//对应输出 4,2,1,3 public static void main(String[] args) { List<Integer> testList = Arrays.asList(3, 9, 10, 4); rank(testList); } public static List<Integer> rank(List<Integer> testList) { if(CollectionUtils.isEmpty(testList)){ return Collections.emptyList(); } final int[] i = {0}; Map<Integer, Integer> map = testList.stream() .sorted((a, b) -> b.compareTo(a)) .collect(Collectors.toMap(t -> t, v -> { final int i1 = ++i[0]; return i1; })); List<Integer> resultList = new ArrayList<>(45); resultList = testList.stream().map(t -> { return map.get(t); }).collect(Collectors.toList()); resultList.forEach(t -> System.out.println(t)); return resultList; } }