How can I sort the value of a Guava Multiset variable and read it?

I have Guava Multiset<Integer>, and I would like to iterate independently through records sorted by (a) the value of the element and (b) the number of elements. I used the simplest way to iterate through a multiset in order of frequency of elements? as

ImmutableMultiset<Integer> entryList = Multisets.copyHighestCountFirst(myIntegerMultiset);
for (Integer i : entryList) {
    System.out.println("I"+i);
}

but this returns all the records, whereas I need a sorted list Multiset.Entry<Integer>(one for a unique value) that will allow me to get the score.

No matter what, I would like to get the same list Multiset.Entry<Integer>sorted by value <Integer>.

+4
source share
2
Iterable<Multiset.Entry<Integer>> entriesSortedByCount = 
   Multisets.copyHighestCountFirst(multiset).entrySet();
Iterable<Multiset.Entry<Integer>> entriesSortedByValue =
   ImmutableSortedMultiset.copyOf(multiset).entrySet();

, entrySet(), Multiset.

+8

, , .

:

Set<Multiset.Entry<Integer>> 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();
    }
}

:

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);

, , .

+2

All Articles