如何对 Pair<String,Integer> 列表进行排序?
How can I sort a List of Pair<String,Integer>?
我有一个 commons 列表 Pair
存储单词及其频率,如下所示
private List<Pair<String, Integer>> words = new ArrayList<Pair<String, Integer>();
我正在尝试对其进行排序,以便当我遍历它以打印单词时,我希望频率最高的单词首先出现。
我尝试着实现 Comparable
,但大多数示例与使用 Pairs
列表不同
您可以使用自定义 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
}
});
按数字降序排列元素
Collections.sort(words, Comparator.comparing(p -> -p.getRight()));
这将按降序使用该对的 "right"。
这使用 Java 8。理论上,您正在装箱值并使用 Integer.compareTo。
但是,通过逃逸分析,可以消除装箱,并且您不会创建任何对象。
您好,我认为这对您有用。
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);
将 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);
如果您只想按频率降序打印值,最简单的方法是:
words.stream()
.sorted(c)
.map(Pair::getKey)
.forEach(System.out::println);
我有一个 commons 列表 Pair
存储单词及其频率,如下所示
private List<Pair<String, Integer>> words = new ArrayList<Pair<String, Integer>();
我正在尝试对其进行排序,以便当我遍历它以打印单词时,我希望频率最高的单词首先出现。
我尝试着实现 Comparable
,但大多数示例与使用 Pairs
您可以使用自定义 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
}
});
按数字降序排列元素
Collections.sort(words, Comparator.comparing(p -> -p.getRight()));
这将按降序使用该对的 "right"。
这使用 Java 8。理论上,您正在装箱值并使用 Integer.compareTo。
但是,通过逃逸分析,可以消除装箱,并且您不会创建任何对象。
您好,我认为这对您有用。
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);
将 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);
如果您只想按频率降序打印值,最简单的方法是:
words.stream()
.sorted(c)
.map(Pair::getKey)
.forEach(System.out::println);