It's not a secret anymore!*
The moose likes Beginning Java and the fly likes Iterators Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Iterators" Watch "Iterators" New topic
Author

Iterators

David Duran
Ranch Hand

Joined: Feb 11, 2002
Posts: 122
If I remove something from i, does it remove that object from the Collection c or the HashMap hashMap?
Collection c = hashMap.values();
Iterator i = c.iterator();
while( i.hasNext() )
{
Object o = i.next();
i.remove();
}
[ March 15, 2002: Message edited by: David Duran ]
Cindy Glass
"The Hood"
Sheriff

Joined: Sep 29, 2000
Posts: 8521
Yes, because the remove() method is actually implemented in the class for the Collection, so it knows how to do it for that Collection
From the API

remove()
Removes from the underlying collection the last element returned by the iterator (optional operation).


"JavaRanch, where the deer and the Certified play" - David O'Meara
Dave Vick
Ranch Hand

Joined: May 10, 2001
Posts: 3244
David
Yes, removing something from an iterator of a collection will remove it from the underlying collection as well. If your looking to get a better understanding of collections and iterators then write a small piece of code to test it.


Dave
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
Errr... let's look at the original question again:
If I remove something from i, does it remove that object from the Collection c or the HashMap hashMap?
It's not just a yes/no question. We've seen now why the Collection is affected - but what about the original HashMap? This hinges on whether the Collection returned by values() is an independent copy of the values in the HashMap, or a linked view. This is addressed in the API for the values() method:
Returns a collection view of the values contained in this map. The collection is backed by the map, so changes to the map are reflected in the collection, and vice-versa. If the map is modified while an iteration over the collection is in progress, the results of the iteration are undefined. The collection supports element removal, which removes the corresponding mapping from the map, via the Iterator.remove, Collection.remove, removeAll, retainAll and clear operations. It does not support the add or addAll operations.

So the remove() operation in this case affects both the Collection returned by values(), and the original HashMap
Of course, the advice about writing a test is always valid. All it takes in this case is adding one System.out.println(hashMap) to the code. Well, two if you want to see "before" and "after".
[ March 15, 2002: Message edited by: Jim Yingst ]

"I'm not back." - Bill Harding, Twister
David Duran
Ranch Hand

Joined: Feb 11, 2002
Posts: 122
Thanks guys for the responses. And thanks Jim for the clarification on the original question, hahah.
I always post my question and write a test for it concurrently because sometimes my tests might be inadequate and I might reach the wrong conclusion because I skipped a base or two. If I reach the same conclusion as the responses, then I can feel more confident in the answer.
The whole Collections framework is new to me so I'm still trying to get a better understanding. I've been living in the world of HashTables and Vectors as the only viable containers, hahaha.
[ March 15, 2002: Message edited by: David Duran ]
David Duran
Ranch Hand

Joined: Feb 11, 2002
Posts: 122
unsyncMap = new HashMap();
hashMap = Collections.synchronizedMap(unsyncMap);
...
Collection c = hashMap.values();
Iterator i = c.iterator();
...
i.next();
...
i.remove();
According to my tests, unsyncMap is the is the underlying collection. Correct or incorrect?
[ March 18, 2002: Message edited by: David Duran ]
 
Consider Paul's rocket mass heater.
 
subject: Iterators
 
Similar Threads
Please Help: Putting Vector objects into HashMap
about instanceof operator
NX: (Contractors) Error handling between lock/unlock
HashMap
To extend or not to extend AbstractCollection