TreeMultiset是否仅保存每个键的重复计数? [英] Does TreeMultiset only holds the count of repeats for each key?

查看:64
本文介绍了TreeMultiset是否仅保存每个键的重复计数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用TreeMultiset尝试了以下代码.似乎"* Twin"条目被丢弃,重复次数存储在TreeMultiset中.我想这是一个功能,而不是一个错误.是否有一个存储每个对象而不是键和重复次数的对象?

I tried the following code with TreeMultiset. Seems like the "*Twin" entries are discarded and the number of repeats are stored in TreeMultiset. I suppose this is a feature and not a bug. Is there an object that stores each object rather than a key and a count of repeats?

public class Guava {

    public static class Obj implements Comparable<Obj> {

        String name;
        int age;

        public Obj(String name, int age) {
            this.name = name;
            this.age = age;
        }

        @Override
        public String toString() {
            return String.format("%s@%d", name, age);
        }

        @Override
        public int compareTo(Obj o) {
            return Integer.compare(age, o.age);
        }
    }

    public static void main(String[] args) {
        TreeMultiset<Obj> tree = TreeMultiset.create();
        tree.add(new Obj("Ajo", 37));
        tree.add(new Obj("AjoTwin", 37));
        tree.add(new Obj("Ari", 31));
        tree.add(new Obj("AriTwin", 31));
        tree.add(new Obj("Fly", 1));
        System.out.println(tree.size());
        for (Obj obj : tree) {
            System.out.println(obj);
        }
    }
}

推荐答案

我们唯一能确定的是:

MultiSet-Docu :请注意,Multiset<E>是不是Map<E, Integer>,尽管这可能是Multiset实现的一部分. Multiset是真正的Collection类型,并且满足所有相关的合同义务.

MultiSet-Docu: Note that Multiset<E> is not a Map<E, Integer>, though that might be part of a Multiset implementation. Multiset is a true Collection type, and satisfies all of the associated contractual obligations.

这意味着MultiSet或多或少是一个同样重要的Set(它没有实现Set).并且Set不包含重复的元素.

That means that MultiSet is more or less a Set (it does not implement Set) that also counts. And a Set contains no duplicate elements.

真正的Set还会保留您插入的第一个元素,并丢弃其他相等的元素.

A true Set also keeps just the first element you inserted and discard any further equal ones.

Set#add(E) :如果此集合已包含该元素,则调用将使该集合保持不变并返回false.

Set#add(E): If this set already contains the element, the call leaves the set unchanged and returns false.

由于TreeMultiSet使用compareTo而不是equals,因此必须最终获得结果.

Since TreeMultiSet uses compareTo instead of equals you'll have to end up with the result you get.

注意:您应将.equals模拟为.compareTo:

TreeMultiSet-Docu :比较必须与Comparable类规范中说明的equals保持一致.否则,生成的多集将违反按Object.equals(java.lang.Object)指定的Collection合同.

TreeMultiSet-Docu: The comparison must be consistent with equals as explained by the Comparable class specification. Otherwise, the resulting multiset will violate the Collection contract, which is specified in terms of Object.equals(java.lang.Object).


如果您想要一些可以存储多个元素的东西,请尝试(List)MultiMap.这基本上是Map<Key, List<Value>>,如果元素具有相同的键,则会自动将它们添加到列表中.


If you want something that can store multiple elements try a (List)MultiMap. That's basically a Map<Key, List<Value>> that automatically adds elements to the list if they have the same key.

这篇关于TreeMultiset是否仅保存每个键的重复计数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆