Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8225339

Optimize HashMap.keySet()/HashMap.values()/HashSet toArray() methods

XMLWordPrintable

    • Icon: Enhancement Enhancement
    • Resolution: Fixed
    • Icon: P4 P4
    • 14
    • 8, 11, 13, 14
    • core-libs
    • None

      Currently HashMap.keySet().toArray(), HashMap.values().toArray(), HashSet.toArray(), LinkedHashMap.keySet().toArray(), LinkedHashMap.values().toArray(), LinkedHashSet.toArray() delegate to AbstractCollection implementation which creates an iterator. This is suboptimal as the corresponding iterator (e.g. HashMap.KeyIterator) should track current state which is quite non-trivial for HashMap. Also modCount is constantly compared with expectedModCount which is also redundant. It seems that existing JIT compilers (C2, Graal) cannot totally eliminate the overhead. I believe that these methods are used quite often (especially HashSet.toArray()) to deserve a specialized implementation similar to LinkedList.toArray().

            tvaleev Tagir Valeev
            tvaleev Tagir Valeev
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved: