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

add asUnmodifiable default methods to collections interfaces

XMLWordPrintable

    • Icon: Enhancement Enhancement
    • Resolution: Not an Issue
    • Icon: P3 P3
    • tbd
    • None
    • core-libs

      Add something like

          default Collection<E> Collection.asUnmodifiable()

      as a default method to the Collection interface that returns an unmodifiable-wrapped instance of that Collection. Covariant overrides would be added for all the collection interfaces: List, Set, Map, Sorted{Map,Set}, Navigable{Map,Set}; and also EnumSet and EnumMap.

      This makes it easier to chain. For example, consider

          List<Type> = input.stream()
              .map(foo)
              .filter(bar)
              .collect(toList());

      To make this unmodifiable, one would have to do

          List<Type> = Collections.unmodifiableList(input.stream()
              .map(foo)
              .filter(bar)
              .collect(toList()));

      or

          List<Type> = input.stream()
              .map(foo)
              .filter(bar)
              .collect(collectingAndThen(toList(), Collections::unmodifiableList);

      It would be nicer to do

          List<Type> = input.stream()
              .map(foo)
              .filter(bar)
              .collect(toList())
              .asUnmodifiable();

      and chaining additional operations to the end would be easier.

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

              Created:
              Updated:
              Resolved: