File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Java in General and the fly likes How To Compare to Iterators...............? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "How To Compare to Iterators...............?" Watch "How To Compare to Iterators...............?" New topic

How To Compare to Iterators...............?

Hanuma Deepak Muvvala
Ranch Hand

Joined: Nov 20, 2005
Posts: 158
Hi All,
Is there any method in collections framework classes to compare two iterators,r else can any body suggest how to compare iterators in java.

Purushoth Thambu
Ranch Hand

Joined: May 24, 2003
Posts: 425
Iterator is just a pointer. I am not sure what you want to compare? If you want to see if both iterator points to same object then == should work. If you want to compare iterators that point to different collection (to check if both the collection contains same elements) then you can use
- containsAll(Collection c) api something like below:

public boolean isEqual(Collection a, Collection b)
return a.containsAll(b) && b.containsAll(a);
Hanuma Deepak Muvvala
Ranch Hand

Joined: Nov 20, 2005
Posts: 158
I have to compare the contents of the lists pointed by these two iterators,i.e. those 2 lists have same values r not.........
Paul Sturrock

Joined: Apr 14, 2004
Posts: 10336

So you mean you want to compare two Lists not two Iterators? Well since a List is a Collection Purushothaman Thambu has already posted the code that lets you do that.

JavaRanch FAQ HowToAskQuestionsOnJavaRanch
Jim Yingst

Joined: Jan 30, 2000
Posts: 18671
Note that the code shown verifies that all elements of a are in b, and all elements of b are in a. You may need to consider: do you care about the order? Is [1, 2, 3] the same as [3, 2, 1]? What about duplicates? Is [1, 2, 2] the same as [1, 2] or [1, 1, 2]? If these lists are considered different, you may wish to use the equals() method already defined in List:

or maybe

It really depends what you want to do...

"I'm not back." - Bill Harding, Twister
I agree. Here's the link:
subject: How To Compare to Iterators...............?
It's not a secret anymore!