-
CSR
-
Resolution: Approved
-
P4
-
None
-
behavioral
-
low
-
-
Java API
-
Implementation
Summary
The toArray() implementations in HashSet, HashMap.keySet(), HashMap.values(), LinkedHashSet, LinkedHashMap.keySet() and LinkedHashMap.values() will no longer delegate to iterator().
Problem
Currently the toArray()
methods of HashSet
, HashMap.keySet()
, HashMap.values()
, LinkedHashSet
, LinkedHashMap.keySet()
and LinkedHashMap.values()
are inherited from AbstractCollection::toArray
which creates an iterator()
. This is unnecessarily expensive, as it maintains extra state in its fields and checks modCount after every element.
Solution
An optimization is proposed to manually implement toArray()
methods of HashSet
, HashMap.keySet()
, HashMap.values()
, LinkedHashSet
, LinkedHashMap.keySet()
and LinkedHashMap.values()
, rather than inherit a less efficient implementation from AbstractCollection
. As a result toArray()
method won't create an iterator()
anymore which may slightly affect the behavior. This is especially important for HashSet
and LinkedHashSet
which could be extended by users. For example, the user might subclass HashSet
, override an iterator()
method adding some custom logic and expect that this custom logic will be executed by toArray()
.
Specification
No specification change; behavioral change only.
- csr of
-
JDK-8225339 Optimize HashMap.keySet()/HashMap.values()/HashSet toArray() methods
- Resolved