Instead of first collecting a stream into a list and then making it unmodifiable, as shown below:
mutableList = list.stream()
                  // perform some stream operations...
                  .filter(myFilterPredicate)
                  .map(myMapperFunction)
                  // collect into a list
                  .collect(toList());
// now make the list unmodifiable
return Collections.unmodifiableList(list);
you can using collectingAndThen as follows:
return list.stream()
           // perform some stream operations...
           .filter(myFilterPredicate)
           .map(myMapperFunction)
           // collect into an unmodifiable list
           .collect(collectingAndThen(toList(), Collections::unmodifiableList));
No comments:
Post a Comment
Note: Only a member of this blog may post a comment.