java 按元素频率的顺序迭代 Multiset 的最简单方法?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4345633/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 05:59:28  来源:igfitidea点击:

Simplest way to iterate through a Multiset in the order of element frequency?

javaguavamultiset

提问by Jonik

Consider this example which prints out some device type stats. ("DeviceType" is an enum with a dozenish values.)

考虑这个打印出一些设备类型统计信息的例子。(“DeviceType”是一个有十几个值的枚举。)

Multiset<DeviceType> histogram = getDeviceStats();
for (DeviceType type : histogram.elementSet()) {
    System.out.println(type + ": " + histogram.count(type));
}

What's the simplest, most elegant way to print the distinct elements in the order of their frequency(most common type first)?

按频率顺序打印不同元素的最简单、最优雅的方法是什么(最常见的类型在前)?

With a quick look at the Multisetinterface, there's no ready-made method for this, and none of Guava's Multisetimplementations (HashMultiset, TreeMultiset, etc) seem to automatically keep elements frequency-ordered either.

随着快速浏览一下Multiset界面,有一个为这个没有现成的方法,并没有番石榴的的Multiset实现(HashMultisetTreeMultiset,等)似乎自动保持要素频率有序无论是。

回答by Louis Wasserman

I just added this feature to Guava, see herefor the Javadoc.

我刚刚将此功能添加到 Guava,请参阅此处获取 Javadoc。

Edit: usage example of Multisets.copyHighestCountFirst()as per the original question:

编辑Multisets.copyHighestCountFirst()根据原始问题的使用示例:

Multiset<DeviceType> histogram = getDeviceStats();
for (DeviceType type : Multisets.copyHighestCountFirst(histogram).elementSet()) {
    System.out.println(type + ": " + histogram.count(type));
}

回答by Sean Patrick Floyd

Here's a method that returns a Listof entries, sorted by frequency (UPDATE: used a flag to toggle ascending / descending order and used Guava's favorite toy: the Enum Singleton Pattern, as found in Effective Java, Item 3 ):

这是一个返回List条目的方法,按频率排序(更新:使用一个标志来切换升序/降序并使用番石榴最喜欢的玩具:the Enum Singleton Pattern,如Effective Java,第 3 项中所述):

private enum EntryComp implements Comparator<Multiset.Entry<?>>{
    DESCENDING{
        @Override
        public int compare(final Entry<?> a, final Entry<?> b){
            return Ints.compare(b.getCount(), a.getCount());
        }
    },
    ASCENDING{
        @Override
        public int compare(final Entry<?> a, final Entry<?> b){
            return Ints.compare(a.getCount(), b.getCount());
        }
    },
}

public static <E> List<Entry<E>> getEntriesSortedByFrequency(
    final Multiset<E> ms, final boolean ascending){
    final List<Entry<E>> entryList = Lists.newArrayList(ms.entrySet());
    Collections.sort(entryList, ascending
        ? EntryComp.ASCENDING
        : EntryComp.DESCENDING);
    return entryList;
}

Test code:

测试代码:

final Multiset<String> ms =
    HashMultiset.create(Arrays.asList(
        "One",
        "Two", "Two",
        "Three", "Three", "Three",
        "Four", "Four", "Four", "Four"
    ));

System.out.println("ascending:");
for(final Entry<String> entry : getEntriesSortedByFrequency(ms, true)){
    System.out.println(MessageFormat.format("{0} ({1})",
        entry.getElement(), entry.getCount()));
}

System.out.println("descending:");
for(final Entry<String> entry : getEntriesSortedByFrequency(ms, false)){
    System.out.println(MessageFormat.format("{0} ({1})",
        entry.getElement(), entry.getCount()));
}

Output:

输出:

ascending:
One (1)
Two (2)
Three (3)
Four (4)
descending:
Four (4)
Three (3)
Two (2)
One (1)

升序:
一 (1)
二 (2)
三 (3)
四 (4)
降序:
四 (4)
三 (3)
二 (2)
一 (1)

回答by Emil

An Implementation using ForwardingMultiSet:

使用ForwardingMultiSet的实现:

(EntryCompfrom seanizer'sanswer)

EntryCompseanizer的答案

enum EntryComp implements Comparator<Multiset.Entry<?>> {
    DESCENDING {
        @Override
        public int compare(final Entry<?> a, final Entry<?> b) {
            return Ints.compare(b.getCount(), a.getCount());
        }
    },
    ASCENDING {
        @Override
        public int compare(final Entry<?> a, final Entry<?> b) {
            return Ints.compare(a.getCount(), b.getCount());
        }
    },
}

public class FreqSortMultiSet<E> extends ForwardingMultiset<E> {
    Multiset<E> delegate;
    EntryComp comp;

    public FreqSortMultiSet(Multiset<E> delegate, boolean ascending) {
        this.delegate = delegate;
        if (ascending)
            this.comp = EntryComp.ASCENDING;
        else
            this.comp = EntryComp.DESCENDING;
    }

    @Override
    protected Multiset<E> delegate() {
        return delegate;
    }

    @Override
    public Set<Entry<E>> entrySet() {
        TreeSet<Entry<E>> sortedEntrySet = new TreeSet<Entry<E>>(comp);
        sortedEntrySet.addAll(delegate.entrySet());
        return sortedEntrySet;
    }

    @Override
    public Set<E> elementSet() {
        Set<E> sortedEntrySet = new LinkedHashSet<E>();
        for (Entry<E> en : entrySet())
            sortedEntrySet.add(en.getElement());
        return sortedEntrySet;
    }

    public static <E> FreqSortMultiSet<E> create(boolean ascending) {
        return new FreqSortMultiSet<E>(HashMultiset.<E> create(), ascending);
    }

    /*
     * For Testing
     * public static void main(String[] args) {
        Multiset<String> s = FreqSortMultiSet.create(false);
        s.add("Hello");
        s.add("Hello");
        s.setCount("World", 3);
        s.setCount("Bye", 5);
        System.out.println(s.entrySet());
    }*/

}

回答by Bozho

Since it is not yet implemented, I guess you can create a Mapwith key=type and value=count. Then sort that map - see here

由于它还没有实现,我猜你可以Map用 key=type 和 value=count创建一个。然后对该地图进行排序 - 请参见此处