tikinghu / google-collections

Automatically exported from code.google.com/p/google-collections
0 stars 0 forks source link

CusomConcurrentHashMap.Values should override toArray(T[]) #305

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
The implementation AbstractCollection.toArray(T[]) in the 1.5 JRE appears
to make the assumption that size() will predict the number of times
getNext() can legally be called on an Iterator returned by iterator().
This does not hold true for Values (or KeySet or EntrySet), and can lead to
a NoSuchElementException in unexpected scenarios, such as the following:

ConcurrentMap<K, T> map = new MapMaker().weakValues().makeMap();
...
ArrayList<T> values = new ArrayList<T>(map.values());

Although this seems to be a bug in the JRE code (and appears to be fixed in
1.6), it could easily be avoided by not depending on the implementation of
toArray() provided by AbstractCollection.

Original issue reported on code.google.com by fay.s...@gmail.com on 8 Dec 2009 at 3:29

GoogleCodeExporter commented 9 years ago
True. I wonder why Sun did not feel it was an important enough fix to backport 
to the 
1.5 tree (it was fixed rather a while ago).

Users experiencing this problem have two workarounds:
1. upgrade, of course
2. use ImmutableList.copyOf() instead

Original comment by kevinb@google.com on 8 Dec 2009 at 5:14

GoogleCodeExporter commented 9 years ago
This issue has been moved to the Guava project (keeping the same id number). 
Simply replace 'google-collections' with 'guava-libraries' in your address 
bar and it should take you there.

Original comment by kevinb@google.com on 5 Jan 2010 at 11:09