commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Michael A. Smith" <...@apache.org>
Subject RE: [Collections] Bug or Feature? - SequencedHashMap iterators do not throw ConcurrentModificationExceptions
Date Wed, 08 May 2002 18:54:59 GMT
On Wed, 8 May 2002, Morgan Delagrange wrote:
> That's my inclination too.  I'm in a real quandry
> about how to handle the LRUMap implementation though. 
> If we make those iterators fail-fast, the following
> operation will produce a
> ConcurrentModificationException:
> 
>   Iterator lruMapKeys = lruMap.keySet().iterator();
> 
>   while (lruMapKeys.hasNext()) {
>     // the second element fails, because
>     // calling get(Object) modifies the underlying
>     // SequencedHashMap
>     System.out.println(lruMap.get(lruMapKeys.next()));
>   }

If you want to iterate over the values to print each of them, use 
values().iterator():

  Iterator lruMapValues = lruMap.values().iterator();

  while(lruMapValues.hasNext()) {
    System.out.println(lruMapValues.next());
  }

Or, if you need to the key and the value, use entrySet().iterator():

  Iterator lruMapEntries = lruMap.entrySet().iterator();

  while(lruMapEntries.hasNext()) {
    Map.Entry entry = (Map.Entry)lruMapEntries.next();
    System.out.println(entry.getValue());
  }

Both of these use the iterator without touching the map, and thus without 
a concurrent modification.

> It's hard to imagine a Map where getting the value of
> a key fails.  Hmm...  Maybe we should change keySet()
> to return a Collection that's not backed by the Map.

No.  keySet() should be backed by the Map.  That's in the Map contract:
http://java.sun.com/j2se/1.3/docs/api/java/util/Map.html#keySet()

regards,
michael

> 
> - Morgan
> 
> --- "Jack, Paul" <pjack@sfaf.org> wrote:
> > My suggestion would be to have the apache
> > collections mimic the
> > java.util collections whereever possible, and so
> > treat this as a
> > bug.  Iterators aren't required to raise
> > ConcurrentModificationExceptions
> > because in theory, a collection (say, one that's
> > backed by a database)
> > might not be able to provide that feature. 
> > Something like SequencedHashMap,
> > which absolutely CAN provide the feature, probably
> > SHOULD provide the 
> > feature...
> > 
> > Furthermore, I seriously doubt that there's any code
> > out there that relies
> > on the fact that SequenceHashMap's iterators don't
> > raise
> > ConcurrentModificationExceptions,
> > and even if such code exists, its behavior is
> > undefined at this point:  It 
> > seems we can fix this in a future release without
> > worrying about breaking 
> > existing code.
> > 
> > Just my 2 cents. 
> > 
> > -Paul
> > 
> > -----Original Message-----
> > From: Morgan Delagrange [mailto:mdelagra@yahoo.com]
> > Sent: Wednesday, May 08, 2002 11:09 AM
> > To: Jakarta Commons Developers
> > Subject: [Collections] Bug or Feature? -
> > SequencedHashMap iterators do
> > not throw ConcurrentModificationExceptions
> > 
> > 
> > Hi all,
> > 
> > In tracking down a bug in LRUMap, I added a new unit
> > test to testMap() to
> > check if all our Map Iterators throw
> > ConcurrentModificationExceptions when
> > the underlying collection is modified. 
> > SequencedHashMap does not.  I know
> > that it's not absolutely _required_ to handle
> > modifications that way, but
> > should we classify this as a bug or an unsupported
> > feature?  It seems risky
> > not to throw a ConcurrentModificationException,
> > because it probably makes
> > the behaviour of the Iterator indeterminate, and in
> > the case of LRUMap I
> > think it's leading to an infinite promotion loop.
> > 
> > - Morgan
> > 
> > 
> > 
> > --
> > To unsubscribe, e-mail:
> > <mailto:commons-dev-unsubscribe@jakarta.apache.org>
> > For additional commands, e-mail:
> > <mailto:commons-dev-help@jakarta.apache.org>
> > 
> > --
> > To unsubscribe, e-mail:  
> > <mailto:commons-dev-unsubscribe@jakarta.apache.org>
> > For additional commands, e-mail:
> > <mailto:commons-dev-help@jakarta.apache.org>
> > 
> 
> 
> =====
> Morgan Delagrange
> http://jakarta.apache.org/taglibs
> http://jakarta.apache.org/commons
> http://axion.tigris.org
> 
> __________________________________________________
> Do You Yahoo!?
> Yahoo! Health - your guide to health and wellness
> http://health.yahoo.com
> 
> --
> To unsubscribe, e-mail:   <mailto:commons-dev-unsubscribe@jakarta.apache.org>
> For additional commands, e-mail: <mailto:commons-dev-help@jakarta.apache.org>
> 


--
To unsubscribe, e-mail:   <mailto:commons-dev-unsubscribe@jakarta.apache.org>
For additional commands, e-mail: <mailto:commons-dev-help@jakarta.apache.org>


Mime
View raw message