使用之间的任何大的区别包含或通过列表循环? [英] Any big difference between using contains or loop through a list?
问题描述
- ArrayList.contains(o)vs foreach | iterator >
- LinkedList.contains(o)vs foreach | iterator
当然,对于foreach | iterator循环,我将不得不显式比较方法,并相应地返回true或false。
我比较的对象是一个对象,其中等于( )
和 hashcode()
都被正确的覆盖。
编辑:
随着问题的新形式不再包括HashMap和TreeMap,我的答案完全不同。我确信其他人已经回答了这个问题,但是在LinkedList和ArrayList中,contains()只是调用indexOf(),它遍历集合。
LinkedList和ArrayList之间,以及contains和foreach之间的性能差别可能都很小,任何大差异。
Performance wise, is there really a big difference between using:
- ArrayList.contains(o) vs foreach|iterator
- LinkedList.contains(o) vs foreach|iterator
Of course, for the foreach|iterator loops, I'll have to explicitly compare the methods and return true or false accordingly.
The object I'm comparing is an object where equals()
and hashcode()
are both properly overridden.
EDIT: Don't need to know about containsValue after all, sorry about that. And yes, I'm stupid... I realized how stupid my question was about containsKey vs foreach, nevermind about that, I don't know what I was thinking. I basically want to know about the ones above (edited out the others).
EDITED:
With the new form of the question no longer including HashMap and TreeMap, my answer is entirely different. I now say no.
I'm sure that other people have answered this, but in both LinkedList and ArrayList, contains() just calls indexOf(), which iterates over the collection.
It's possible that there are tiny performance differences, both between LinkedList and ArrayList, and between contains and foreach, there aren't any big differences.
这篇关于使用之间的任何大的区别包含或通过列表循环?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!