nulakasatish / guava-libraries

Automatically exported from code.google.com/p/guava-libraries
Apache License 2.0
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

Original comment by kevinb@google.com on 18 Jun 2010 at 12:04

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:16

GoogleCodeExporter commented 9 years ago

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