Java中按相关列表排序的订单 [英] Order list by a related-list in Java
本文介绍了Java中按相关列表排序的订单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有两个列表:
List<Double> nums = Arrays.asList(5.0, 0.9, 10.4);
List<Double> mappedNums = Arrays.asList(2.1, 0.3, 1.2);
我想根据mappedNums
对nums
进行排序:我希望nums
为[0.9, 10.4, 5.0]
,因为0.9
是nums
的第二个元素,而<nums
是nums
的第二个元素c1>是最小的,依此类推.我该怎么办?
I would like to sort nums
according to mappedNums
: I would like to nums
to be [0.9, 10.4, 5.0]
, since the 0.9
is the second element of nums
, and the second element of mappedNums
is the smallest, and so on. How do I do that?
推荐答案
IntStream.range(0, nums.size())
.mapToObj(x -> new SimpleEntry<>(x, nums.get(x)))
.sorted(Comparator.comparingDouble(x -> mappedNums.get(x.getKey())))
.map(Entry::getValue)
.forEachOrdered(System.out::println);// 0.9 10.4 5.0
这篇关于Java中按相关列表排序的订单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文