• Type: Sub-task
    • Status: Open
    • Priority: P3
    • Resolution: Unresolved
    • Affects Version/s: 10
    • Fix Version/s: tbd_major
    • Component/s: core-libs
    • Labels:


      Probably something like this on List:

          static <T> List<T> copyOf(Collection<? extends T> coll)

      This would return an immutable List. There would be similar factory methods for Set and Map. This would be implemented the obvious way for most cases.

      In addition, this could check the implementation of the collection being copied from, and if it's a class that's known to be immutable, it would simply return that reference. A fast path special case could also be made for common collections such as ArrayList, where a minimal number of copies could be made.

      This would be a shallow copy.

      This would also variant aliasing of collections, which generally isn't permitted. For example,

          List<String> list = List.of(...);
          List<CharSequence> newList = List.copyOf(list);

      This generally isn't permitted, since it can lead to heap pollution, but since these implementations are immutable, this should be safe.

      Short-circuiting of copying is one of the oft-cited use cases for exposing immutable collection types. Implementing this could mitigate some of the demand for doing this. However, other use cases for exposing immutable types should still be considered.


        There are no comments yet on this issue.


          • Assignee:
            smarks Stuart Marks
            smarks Stuart Marks
          • Votes:
            0 Vote for this issue
            1 Start watching this issue


            • Created: