Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/361.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 从Google Collections中查找多集中的前N个元素?_Java_Guava_Multiset - Fatal编程技术网

Java 从Google Collections中查找多集中的前N个元素?

Java 从Google Collections中查找多集中的前N个元素?,java,guava,multiset,Java,Guava,Multiset,A是一组元素,每个元素都有一个计数(即可能出现多次) 我不能告诉你我有多少次想做下面的事情 制作柱状图(精确的多集) 从直方图中按计数获取前N个元素 示例:前10个URL(按提到的次数),前10个标记(按应用的次数) 给定Google Collections Multiset,规范的方法是什么 是一篇关于它的博客文章,但这段代码并不是我想要的。首先,它返回所有内容,而不仅仅是top N。其次,它复制(有可能避免复制吗?)。第三,我通常需要一种确定性排序,即如果计数相等,则为tiebreak。其他

A是一组元素,每个元素都有一个计数(即可能出现多次)

我不能告诉你我有多少次想做下面的事情

  • 制作柱状图(精确的多集)
  • 从直方图中按计数获取前N个元素
  • 示例:前10个URL(按提到的次数),前10个标记(按应用的次数)

    给定Google Collections Multiset,规范的方法是什么


    是一篇关于它的博客文章,但这段代码并不是我想要的。首先,它返回所有内容,而不仅仅是top N。其次,它复制(有可能避免复制吗?)。第三,我通常需要一种确定性排序,即如果计数相等,则为tiebreak。其他缺点:它不是静态的,等等。

    为了让人们从另一个角度发表评论,我将发布我引用的博客文章的一个稍加修改的版本:

    package com.blueshiftlab.twitterstream.summarytools;
    
    import com.google.common.collect.ImmutableList;
    import com.google.common.collect.Multiset;
    import com.google.common.collect.Ordering;
    import com.google.common.collect.Multiset.Entry;
    
    public class Multisets {
        // Don't construct one
        private Multisets() {
        }
    
        public static <T> ImmutableList<Entry<T>> sortedByCount(Multiset<T> multiset) {
            Ordering<Multiset.Entry<T>> countComp = new Ordering<Multiset.Entry<T>>() {
                public int compare(Multiset.Entry<T> e1, Multiset.Entry<T> e2) {
                    return e2.getCount() - e1.getCount();
                }
            };
            return countComp.immutableSortedCopy(multiset.entrySet());
        }
    
        public static <T> ImmutableList<Entry<T>> topByCount(Multiset<T> multiset,
                int max) {
            ImmutableList<Entry<T>> sortedByCount = sortedByCount(multiset);
            if (sortedByCount.size() > max) {
                sortedByCount = sortedByCount.subList(0, max);
            }
    
            return sortedByCount;
        }
    }
    
    package com.blueshiftlab.twitterstream.summarytools;
    导入com.google.common.collect.ImmutableList;
    导入com.google.common.collect.Multiset;
    导入com.google.common.collect.Ordering;
    导入com.google.common.collect.Multiset.Entry;
    公共类多重集{
    //不要建造一个
    私有多集(){
    }
    公共静态不可变列表sortedByCount(多集多集){
    排序countComp=新排序(){
    公共整数比较(多集项e1、多集项e2){
    返回e2.getCount()-e1.getCount();
    }
    };
    返回countComp.immutableSortedCopy(multiset.entrySet());
    }
    公共静态不可变列表topByCount(多集多集,
    整数(最大值){
    ImmutableList sortedByCount=sortedByCount(多集);
    if(sortedByCount.size()>max){
    sortedByCount=sortedByCount.子列表(0,最大值);
    }
    返回分类计数;
    }
    }
    
    我编写的方法具有您所要求的基本功能,只是它们执行复制并且缺少确定性的中断逻辑。它们目前是谷歌内部的,但我们可能会在某个时候将其开源。这个番石榴有方法签名

    他们的算法类似于博客文章:对条目列表进行排序。使用更好的方法会更快,但更复杂


    编辑:从Guava 11开始,如果我理解正确,这个解决方案将在每次您想要检索前N个元素时复制并排序整个集合。我不确定您的要求是什么,但堆排序解决方案在时间和空间上都优于此,因此我不确定其好处是什么。您正在优化速度,我正在寻找最少的由我编写的代码行。我明白了-您的帖子中不清楚这一点,特别是因为您要求避免复制。小心,您的比较器按计数下降良好点排序。这是故意的,但不是明确的。“top N”通常表示降序排序。如何使用它来获取top N元素?