如何以相反的顺序对IntStream进行排序 [英] How to sort an IntStream in reverse order
本文介绍了如何以相反的顺序对IntStream进行排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在使用 BufferedReader
从.txt文件中读取数字。我想颠倒这种蒸汽中元素的顺序,这样当它们被收集时,它们将从最高到最低排列。我不想在构建数组之后进行排序,因为我不知道它中可能有多少个元素,我只需要最多的N个元素。
I'm reading in numbers from a .txt file using BufferedReader
. I want to reverse the order of elements in this steam so that when they are collected they will be arranged from the highest to the lowest. I don't want to sort after the array has been built because I have no idea how many elements might be in it, I only need the highest N elements.
in = new BufferedReader(reader);
int[] arr = in.lines()
.mapToInt(Integer::parseInt)
.sorted()
.limit((long) N)
.toArray();
推荐答案
在排序和否定之前尝试否定值(返回正常)排序后:
Try negating the values before sorting and negating (back to normal) after sorting:
in = new BufferedReader(reader);
int[] arr = in.lines()
.mapToInt(Integer::parseInt)
.map(i -> -i).sorted().map(i -> -i)
.limit((long) N)
.toArray();
这篇关于如何以相反的顺序对IntStream进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文