• Sub-task
    • Resolution: Fixed
    • P4
    • 9
    • None
    • core-libs


      Explore the potential uses of convenience Factory Methods for Collections (List.of(), Map.ofEntries(), etc) within the JDK.

      There are probably places in the JDK where collections are created and initialized with contents and are thereafter not changed. They're effectively immutable. They might or might not be wrapped with the unmodiable wrappers. But, searching for calls to Collections.unmodifiable*() might prove fruitful at turning up locations where the convenience factories could be used.

      There are probably a bunch of places where lists are initialized using Arrays.asList(). It might be reasonable to look for these locations and to investigate replacing them with calls to List.of().

      Bear in mind that the immutable collections returned by the convenience factories have different semantics from collections wrapped with the unmodifiable wrappers. One is that the original collections are still modifiable by anyone who keeps a reference to them. Another is that the iteration order of the new sets and maps differs (in fact, the iteration order changes randomly).

      The List returned by Arrays.asList() is actually mutable, but it cannot change size (i.e., no adds or removes are permitted). However, the List returned by List.of() is truly immutable. In addition, calling List.of() unavoidably creates a copy of its input array, for sizes larger than 2, which may be a performance concern.

      These issues represent potential incompatibilities and must be investigated or tested. Therefore, while the convenience factories are probably useful in many cases, they aren't necessarily a drop-in replacement for Collections.unmodifiable*(...) and Arrays.asList().




            smarks Stuart Marks
            smarks Stuart Marks
            0 Vote for this issue
            3 Start watching this issue