Java 如何对 Pair<String,Integer> 列表进行排序?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29920027/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I sort a List of Pair<String,Integer>?
提问by code511788465541441
I have a list of commmons Pair
that stores words and their frequency like the following
我有一个Pair
存储单词及其频率的公共列表,如下所示
private List<Pair<String, Integer>> words = new ArrayList<Pair<String, Integer>();
I am trying to sort it so that when I iterate over it to print the words, I want the words with the highest frequency to appear first.
我正在尝试对其进行排序,以便当我对其进行迭代以打印单词时,我希望频率最高的单词首先出现。
I tried playing around with implementing Comparable
but most examples are not similar to using a list of Pairs
我尝试尝试实现,Comparable
但大多数示例与使用 Pairs 列表不同
采纳答案by code511788465541441
You can use a custom Comparator
:
您可以使用自定义Comparator
:
Collections.sort(words, new Comparator<Pair<String, Integer>>() {
@Override
public int compare(final Pair<String, Integer> o1, final Pair<String, Integer> o2) {
// TODO: implement your logic here
}
});
回答by Peter Lawrey
To sort the elements by decreasing order of number
按数字的降序对元素进行排序
Collections.sort(words, Comparator.comparing(p -> -p.getRight()));
This will use the "right" of the pair in descending order.
这将按降序使用该对的“右边”。
This uses Java 8. Notionally you are boxing the value and using Integer.compareTo.
这使用 Java 8。从理论上讲,您正在对值进行装箱并使用 Integer.compareTo。
However, with escape analysis, the boxing can be eliminated and you mgiht not be creating any objects.
但是,通过逃逸分析,可以消除拳击,并且您不会创建任何对象。
回答by Greg King
Hello i think this should work for you.
您好,我认为这应该适合您。
List<Pair<String, Integer>> words = new ArrayList<Pair<String, Integer>>();
words.add(new Pair<String, Integer>("hello",2));
words.add(new Pair<String, Integer>("hello",1));
words.add(new Pair<String, Integer>("aello",3));
words.sort(new Comparator<Pair<String, Integer>>() {
@Override
public int compare(Pair<String, Integer> o1, Pair<String, Integer> o2) {
if (o1.getValue() > o2.getValue()) {
return -1;
} else if (o1.getValue().equals(o2.getValue())) {
return 0; // You can change this to make it then look at the
//words alphabetical order
} else {
return 1;
}
}
});
System.out.println(words);
回答by Boris the Spider
Use a Java 8 lambda in combination with Comparator.comparing
(you also need to reverse the order):
结合使用 Java 8 lambda Comparator.comparing
(您还需要颠倒顺序):
import static java.util.Collections.reverseOrder;
import static java.util.Comparator.comparing;
final List<Pair<String, Integer>> words = new ArrayList<>();
final Comparator<Pair<String, Integer>> c = reverseOrder(comparing(Pair::getValue));
Collections.sort(words, c);
Easiest way if you just want to print the values in descending order of frequency:
如果您只想按频率降序打印值,最简单的方法是:
words.stream()
.sorted(c)
.map(Pair::getKey)
.forEach(System.out::println);