/**
* 等值排名算法
*
* @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;
}
}