使用番石榴多集或Collections.frequency()? [英] Using Guava multisets or Collections.frequency()?

查看:54
本文介绍了使用番石榴多集或Collections.frequency()?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 多集 可轻松访问元素的频率,但我意识到存在解决方案

Java HashMap如何使用相同的哈希码处理不同的对象?.

番石榴代码如下

  public int count(@Nullable Object element){计数频率= Maps.safeGet(backingMap,element);返回(频率==空)?0:frequency.get();} 

类似地,对于维持其元素顺序并由AVL树支持的 TreeMultiset ,可以在O(log(n))中获得 count 步骤而不是O(n),其中n是集合的大小.番石榴代码如下:

  public int count(@Nullable Object element){尝试 {@SuppressWarnings(未选中")E e =(E)元素;AvlNode< E>root = rootReference.get();如果(!range.contains(e)|| root == null){返回0;}返回root.count(comparator(),e);} catch(ClassCastException e){返回0;} catch(NullPointerException e){返回0;}} 

I was using Multiset to have easy access to the freq of elements, but I realize there is Collections#frequency(Collection<?>, Object) that does the same for any collection. What is the point of using Multiset then? Is performance an issue here?

解决方案

Guava documentation for Multiset#count() has to say:

Note that for an Object.equals(java.lang.Object)-based multiset, this gives the same result as Collections.frequency(java.util.Collection, java.lang.Object) (which would presumably perform more poorly).

So, yes, I suspect that performance is the issue here.

I think Multiset#count is more efficient because Collections#frequency iterates through the entire collection. For an object o whose frequency you're checking, it goes through all elements e in the collection and checks (o == null ? e == null : o.equals(e)).

For Multiset (which is an interface), the exact implementation of count depends on the class. If it is a HashMultiset, for example, then it is backed by a HashMap. For details about how that is more efficient than iterating through the whole collection, take a look at this answer: How does a Java HashMap handle different objects with the same hash code?.

The Guava code is as follows

public int count(@Nullable Object element) {
    Count frequency = Maps.safeGet(backingMap, element);
    return (frequency == null) ? 0 : frequency.get();
}

Similarly, for a TreeMultiset, which maintains the ordering of its elements and is backed by an AVL tree, count can be obtained in O(log(n)) steps instead of O(n), where n is the size of the collection. The Guava code is as follows:

public int count(@Nullable Object element) {
    try {
      @SuppressWarnings("unchecked")
          E e = (E) element;
          AvlNode<E> root = rootReference.get();
          if (!range.contains(e) || root == null) {
              return 0;
          }
      return root.count(comparator(), e);
    } catch (ClassCastException e) {
          return 0;
    } catch (NullPointerException e) {
          return 0;
    }
}

这篇关于使用番石榴多集或Collections.frequency()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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