简体   繁体   English

Java中ContainsAll的成本是多少?

[英]What is the cost of ContainsAll in Java?

I discovered containsAll() (a List interface method) during some coding today, and it looks pretty slick. 我今天在一些编码中发现了containsAll() (一种List接口方法),它看起来很漂亮。 Does anyone know how much this costs in terms of performance/iterations? 有谁知道在性能/迭代方面要花多少钱?

The documentation didn't offer much in terms of that. 文档在这方面没有提供太多帮助。

  • first, it iterates each element of the supplied collection 首先,迭代提供的集合的每个元素
  • then it iterates all elements of the list and compares the current element with them using .equals(..) (Note: this is about lists, as you specified in the question. Other collections behave differently) 然后迭代列表中的所有元素,并使用.equals(..)将当前元素与它们进行比较(注意:正如您在问题中所指定的,这是关于列表的。其他集合的行为有所不同)

So it's O(n*m), where n and m are the sizes of both collections. 因此它是O(n * m),其中n和m是两个集合的大小。

public boolean containsAll(Collection<?> c) {
    Iterator<?> e = c.iterator();
    while (e.hasNext())
        if (!contains(e.next()))
        return false;
    return true;
}

Use the source, Luke :) 使用源卢克:)

Edit: As Bozho pointed out, you're asking about List.containsAll() which overrides Collection.containsAll() . 编辑:正如Bozho所指出的,您正在询问List.containsAll() ,它重写了Collection.containsAll() The following ramblings are mainly concerned with the latter: 以下是有关后者的主要讨论:

Most Collection classes will use the implementation of containsAll from AbstractCollection , which does it like this: 大多数Collection类将使用AbstractCollectioncontainsAll实现 ,其实现方式如下:

public boolean containsAll(Collection<?> c) {
    for (Object e : c)
        if (!contains(e))
            return false;
    return true;
}

There's no guarantee that some implementation does it completely differently, though -- which could result in either better or worse runtime behavior. 但是,不能保证某些实现会完全不同地执行它-这可能导致更好或更坏的运行时行为。

The above implementation of containsAll will be at least O(n) where n is the number of items in the Collection parameter you pass in, plus whatever time contains takes: 上述实施containsAll将至少O(n),其中n是项目的数量Collection你传递参数再加上任何时间contains需要:

  • For a HashSet / HashMap this might be O(1) (best case, no collisions), so the overall runtime of containsAll would still be O(n) 对于HashSet / HashMap它可能是O(1)(最好的情况下,没有冲突),因此containsAll的整体运行时间仍然是O(n)
  • For an ArrayList , contains will take O(m) where m is the number items in the list (not the parameter), so the overall time for containsAll would be O(n*m) 对于ArrayListcontains将采用O(m),其中m是列表中的数字项(不是参数),所以containsAll的总时间为O(n * m)

if you are calling A.containsAll(B) 如果您正在呼叫A.containsAll(B)

openjdk iterates all elements of B calling A.contains(b). openjdk迭代B的所有调用A.contains(b)的元素。

A.contains(b) iterates all elements of A calling a.equals(b) A.contains(b)迭代A的所有元素,调用a.equals(b)

This is taken from source for open jdk 7 这是从开源jdk 7获取的

consider 考虑

n.ContainsAll(m) n。包含全部(m)

the best possible case scenario is O(m), and that's if n is a perfect hash set. 最好的情况是O(m),即n是一个完美的哈希集。

considering unsorted lists, i can come up with an O(n*log(n) + m*log(m)) algorithm 考虑到未排序的列表,我可以提出O(n * log(n)+ m * log(m))算法

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM