[英]Sorting a list of triples within a map using one of the values of the triple
I have a hashmap with a triplet like this:我有一个像这样的三元组的哈希图:
HashMap<String, List<Triplet<String,Integer,Integer>>> output = new HashMap<String, List<Triplet<String,Integer,Integer>>>();
Where the key is a file name and triplet contains information of a word and its frequency information(tf and df).Eg其中键是文件名,三元组包含单词的信息及其频率信息(tf 和 df)。例如
{File1 = {coffee,23,1},{caffeine,12,2},{brew,9,1}; File2 = {}.......}
I want to sort the values of this list of triples according to the second value of the triplet ie tf (term frequency).我想根据三元组的第二个值,即 tf(词频)对这个三元组列表的值进行排序。
for (Entry<String, List<Triplet<String, Integer, Integer>>> entry : output.entrySet()) {
String key = entry.getKey();
List<Triplet<String, Integer, Integer>> value = entry.getValue();
What will come next?接下来会发生什么?
Here's what Triplet looks like:这是三胞胎的样子:
public class Triplet<T, U, V> {
private final T first;
private final U second;
private final V third;
public Triplet(T first, U second, V third) {
this.first = first;
this.second = second;
this.third = third;
}
public T getFirst() {
return first;
}
public U getSecond() {
return second;
}
public V getThird() {
return third;
}
}
Any help, guidance will be appreciated.任何帮助,指导将不胜感激。
There are 2 ways than came into my mind:我想到了两种方法:
make your Triplet
Comparable
让你的Triplet
Comparable
write a Comparator
写一个Comparator
Than you can either sort the List or use eg a TreeSet
比您可以对 List 进行排序或使用例如TreeSet
If you can not change your Triplet
class by implementing Comparable
on it, this would be a solution, using the Collections.sort
method:如果你不能通过在它上面实现Comparable
来改变你的Triplet
类,这将是一个解决方案,使用Collections.sort
方法:
for (Entry<String, List<Triplet<String, Integer, Integer>>> entry : output.entrySet()) {
String key = entry.getKey();
List<Triplet<String, Integer, Integer>> value = entry.getValue();
// This sorts the list using the anonymous instantiated Comparator class
Collections.sort(value, new Comparator<Triplet<String, Integer, Integer>>(){
@Override
public int compare(Triplet<String, Integer, Integer> object1, Triplet<String, Integer, Integer> object2) {
return object1.getSecond().compareTo(object2.getSecond());
}
});
Or, in a more generic way, without using hard types:或者,以更通用的方式,不使用硬类型:
Collections.sort(value, new Comparator<Triplet>(){
@Override
public int compare(Triplet object1, Triplet object2) {
return object1.getSecond().compareTo(object2.getSecond());
}
});
What about this?那这个呢?
public class Triplet<T, U, V> implements Comparable<Triplet<T, U, V> > {
private final T first;
private final U second;
private final V third;
public Triplet(T first, U second, V third) {
this.first = first;
this.second = second;
this.third = third;
}
public T getFirst() {
return first;
}
public U getSecond() {
return second;
}
public V getThird() {
return third;
}
public int compareTo(Triplet<T, U, V> comparetrpl) {
T a = comparetrpl.getFirst();
//ascending order
return (this.first > a) ? 1 : 0;
//descending order
//return (a > this.first) ? 1 : 0;
}
}
And the use sort
method?以及使用sort
方法?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.