add asUnmodifiable default methods to collections interfaces

XMLWordPrintable

    • Type: Enhancement
    • Resolution: Not an Issue
    • Priority: P3
    • tbd
    • Affects Version/s: None
    • Component/s: 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.

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

              Created:
              Updated:
              Resolved: