使用之间的任何大的区别包含或通过列表循环? [英] Any big difference between using contains or loop through a list?

查看:122
本文介绍了使用之间的任何大的区别包含或通过列表循环?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

性能方面,使用之间真的有很大的区别:


  • ArrayList.contains(o)vs foreach | iterator
  • >
  • LinkedList.contains(o)vs foreach | iterator



当然,对于foreach | iterator循环,我将不得不显式比较方法,并相应地返回true或false。

我比较的对象是一个对象,其中等于( ) hashcode()都被正确的覆盖。

毕竟不需要知道有关containsValue,对此抱歉。是的,我是愚蠢的...我意识到我的问题是关于containsKey与foreach是多么愚蠢,我不知道我在想什么。我基本上想知道上面那些(编辑了其他人)。

解决方案

编辑:



随着问题的新形式不再包括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屋!

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