Java中按相关列表排序的订单 [英] Order list by a related-list in Java

查看:121
本文介绍了Java中按相关列表排序的订单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个列表:

List<Double> nums = Arrays.asList(5.0, 0.9, 10.4);
List<Double> mappedNums = Arrays.asList(2.1, 0.3, 1.2);

我想根据mappedNumsnums进行排序:我希望nums[0.9, 10.4, 5.0],因为0.9nums的第二个元素,而<numsnums的第二个元素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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆