Java – what’s the big difference between using include or loop through lists?

Performance wise, there is really a big difference between use:

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

Of course, for the foreach | iterator loop, I must explicitly compare methods and return true or false accordingly

The object I'm comparing is the object that both equals () and hashcode () are correctly overwritten

Editor: after all, I don't need to know about containsvalue. Sorry Yes, I'm stupid... I realize my question is about containskey vs foreach. I don't know what I'm thinking I basically want to know the above (other edited ones)

Solution

Edit:

With the new form of the question no longer including HashMap and treemap, my answer is completely different I say no now

I'm sure others have answered this, but in LinkedList and ArrayList, contains () just calls indexof (), which traverses the collection

It is possible that there are small performance differences between LinkedList and ArrayList, and between inclusion and foreach, without any big differences

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>