dnrajugade / guava-libraries

Automatically exported from code.google.com/p/guava-libraries
Apache License 2.0
0 stars 0 forks source link

Performance of Iterables/Iterators.elementsEqual() #1183

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
Iterables.elementsEqual(Iterable<?>, Iterable<?>) can check sizes of iterables 
before comparing elements if iterables are Collections actually.

Iterators.elementsEqual(Iterator<?>, Iterator<?>) can check if passed iterators 
are same (==) to avoid iteration and equality checking.

Original issue reported on code.google.com by Ash2kk@gmail.com on 1 Nov 2012 at 9:15

GoogleCodeExporter commented 9 years ago
Iterators.elementsEqual will behave *very* differently than you expect if you 
pass in the same iterator, since both of them will be calling next() on the 
same iterator.

(Indeed, we should reject == arguments out of hand, no?)

Original comment by lowas...@google.com on 1 Nov 2012 at 3:13

GoogleCodeExporter commented 9 years ago
Well, unless you want to allow one to verify that a single iterator contains 
successive pairs of equal elements... Nah let's not. ;)

Original comment by stephan...@gmail.com on 1 Nov 2012 at 4:07

GoogleCodeExporter commented 9 years ago
Submitted change internally; should be mirrored out soon.

We decided not to change Iterators.elementsEqual, because -- among other things 
-- many empty collections return Iterators.emptyIterator(), which is of course 
== to itself.  

We considered rejecting == nonempty iterators, but decided it probably wasn't 
worth the complication, since you'd almost never encounter that case unless you 
were doing so deliberately.

Original comment by lowas...@google.com on 7 Nov 2012 at 8:33

GoogleCodeExporter commented 9 years ago

Original comment by lowas...@google.com on 7 Nov 2012 at 8:33

GoogleCodeExporter commented 9 years ago
Maybe it can be a bad idea to check size() of a Collection if this is a 
FilteredCollection, returned by Collections2.filter(), what do you think? If 
compared collections have same size then such check will result in double 
filtering in worst case. Maybe at least document it?

Original comment by Ash2kk@gmail.com on 13 Nov 2012 at 4:51

GoogleCodeExporter commented 9 years ago
This issue has been migrated to GitHub.

It can be found at https://github.com/google/guava/issues/<id>

Original comment by cgdecker@google.com on 1 Nov 2014 at 4:13

GoogleCodeExporter commented 9 years ago

Original comment by cgdecker@google.com on 3 Nov 2014 at 9:08