Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/sorting/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/facebook/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何根据输入值和计数对番石榴多集进行排序?_Java_Sorting_Guava_Multiset - Fatal编程技术网

Java 如何根据输入值和计数对番石榴多集进行排序?

Java 如何根据输入值和计数对番石榴多集进行排序?,java,sorting,guava,multiset,Java,Sorting,Guava,Multiset,我有一个番石榴Multiset,我想独立地遍历(a)元素值和(b)元素计数排序的条目。我用过 ImmutableMultiset entryList=multiset.copyHighestCountFirst(myIntegerMultiset); for(整数i:entryList){ System.out.println(“I”+I); } 但这将返回所有条目,而我希望得到一个排序的Multiset.Entry(每个唯一值一个)列表,这将允许我获得计数 独立地,我想获得相同的多集条目列表

我有一个番石榴
Multiset
,我想独立地遍历(a)元素值和(b)元素计数排序的条目。我用过

ImmutableMultiset entryList=multiset.copyHighestCountFirst(myIntegerMultiset);
for(整数i:entryList){
System.out.println(“I”+I);
}
但这将返回所有条目,而我希望得到一个排序的
Multiset.Entry
(每个唯一值一个)列表,这将允许我获得计数

独立地,我想获得相同的
多集条目列表,该列表按
的值排序

Iterable entriesSortedByCount=
multiset.copyHighestCountFirst(multiset.entrySet();
Iterable entriesSortedByValue=
ImmutableSortedMultiset.copyOf(multiset.entrySet();

基本上,您只需要
entrySet()
,而不是迭代
Multiset
本身。

您可以获得一组条目,然后根据需要对其进行排序

以下是获取一组条目的方法:

Set<Multiset.Entry<Integer>> entries = myIntegerMultiset.entrySet();
Set entries=myIntegerMultiset.entrySet();
然后为了排序,我们可以定义两个比较器:

Comparator<Multiset.Entry<Integer>> byCount = new Comparator<Multiset.Entry<Integer>>() {
    int compare(Multiset.Entry<Integer> e1, Multiset.Entry<Integer> e2) {
        return e2.getCount() - e1.getCount();
    }
}

Comparator<Multiset.Entry<Integer>> byValue = new Comparator<Multiset.Entry<Integer>>() {
    int compare(Multiset.Entry<Integer> e1, Multiset.Entry<Integer> e2) {
        return e2.getElement() - e1.getElement();
    }
}
comparatorbycount=新的比较器(){
int比较(多集项目e1,多集项目e2){
返回e2.getCount()-e1.getCount();
}
}
比较器byValue=新比较器(){
int比较(多集项目e1,多集项目e2){
返回e2.getElement()-e1.getElement();
}
}
然后,您可以向树集提供比较器,以获得已排序的集合:

Set<Multiset.Entry<Integer>> entries = myIntegerMultiset.entrySet();
Set<Multiset.Entry<Integer>> entriesSortedByCount = Sets.newTreeset(byCount);
entriesSortedByCount.addAll(entries);
Set<Multiset.Entry<Integer>> entriesSortedByValue = Sets.newTreeset(byValue);
entriesSortedByValue.addAll(entries);
Set entries=myIntegerMultiset.entrySet();
Set entriesSortedByCount=Set.newTreeset(按计数);
entriesSortedByCount.addAll(条目);
Set entriesSortedByValue=Set.newTreeset(byValue);
entriesSortedByValue.addAll(条目);

实际上,Louis Wasserman的答案要好得多,但如果您想自定义排序,我也会发布这一条。

第二个比较器是危险的——如果
整型值可以是正的,也可以是负的,溢出可能会导致不正确的比较。如果您使用Java 7,则更喜欢
int.compare(int,int)
,或者
Integer.compare
。(在第一种情况下没关系,因为减去非负值永远不会溢出,但值得警惕。)+1因为知道比较器方法很有用(并且知道危险)。+1因为我被困在一个只能访问Guava 13的环境中,而Guava 13没有
copyHighestCountFirst()
method。您正在创建原始multiset的SortedMultiset副本,该副本按元素的值对元素进行排序,然后从中获取entrySet。这就是你所困惑的吗?我想现在已经很清楚了——复制操作是构造ImmutableSortedMultiset的方法,它可能没有公共构造函数。创建的动作会自动启动排序。正确。它就像一个
TreeSet
,其中
新的TreeSet(集合)
自动创建集合的一个排序
TreeSet
副本,除了带有一个更具描述性的名称,包括单词“copy”。从javadoc注释来看,它看起来像
copyHighestCountFirst()的elementSet()顺序
map是一个可能随时消失的实现细节
copyHighestCountFirst
仅保证迭代器()的顺序,而不是
entrySet()
顺序。来自
Multiset#entrySet()
javadoc:
…元素集中元素的顺序未指定。
来自ImmutableCollection Javadocs:“确定性迭代。迭代顺序始终定义良好,具体取决于集合的创建方式(有关详细信息,请参阅相应的工厂方法).View集合,如Multiset.elementSet()以与父集合相同的顺序迭代,除非另有说明。“
Comparator<Multiset.Entry<Integer>> byCount = new Comparator<Multiset.Entry<Integer>>() {
    int compare(Multiset.Entry<Integer> e1, Multiset.Entry<Integer> e2) {
        return e2.getCount() - e1.getCount();
    }
}

Comparator<Multiset.Entry<Integer>> byValue = new Comparator<Multiset.Entry<Integer>>() {
    int compare(Multiset.Entry<Integer> e1, Multiset.Entry<Integer> e2) {
        return e2.getElement() - e1.getElement();
    }
}
Set<Multiset.Entry<Integer>> entries = myIntegerMultiset.entrySet();
Set<Multiset.Entry<Integer>> entriesSortedByCount = Sets.newTreeset(byCount);
entriesSortedByCount.addAll(entries);
Set<Multiset.Entry<Integer>> entriesSortedByValue = Sets.newTreeset(byValue);
entriesSortedByValue.addAll(entries);