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

Please add java.util.Stream.ofNullable(T object)

    XMLWordPrintable

Details

    • b51
    • generic
    • generic

    Backports

      Description

        A DESCRIPTION OF THE REQUEST :
        Please add a method to the java.util.Stream interface similar to:

             public static <T> Stream<T> ofNullable(T object) {
                  return object != null ? Stream.of(object) : Stream.empty();
             }

        (Note: The name 'ofNullable' above is chosen to mimic Optional.ofNullable(T) which performs a similar function. However, another name might be more amenable to being used via 'static import' for further savings in code size)

        JUSTIFICATION :
        While refactoring several moderately large codebases to use Java 8 features, I have encountered situations many times where a possibly-null object must be fetched, and then converted into a stream (or an empty stream for null) for later processing.

        A typical case where this occurs is during a call to Stream.flatMap() when the implementation must call Map.get() to look up an object and then return a stream of the returned object (if it was found in the map) or an empty stream otherwise.

        Currently, this requires some boilerplate code, which makes readability more difficult. More annoying still, the boilerplate code often requires making two references to the value to be converted, which means that if the value is the result of a performance-intensive expression being evaluated (a map lookup, etc.), the result of the expression has to be assigned to a temporary variable first, further complicating the code.



        EXPECTED VERSUS ACTUAL BEHAVIOR :
        EXPECTED -
        I would like to be able to write code like this:
             SomeCollection<String> collection = ...;
             Map<String, Integer> map = ...;
             List<Integer> result = collection.stream()
                  .flatMap(str -> Stream.ofNullable(map.get(str)))
                  .collect(Collectors.toList());

        ACTUAL -
        I currently have to write code like this:

             SomeCollection<String> collection = ...;
             Map<String, Integer> map = ...;
             return collection.stream().flatMap(str -> {
                  Integer temp = map.get(str);
                  return temp != null ? Stream.of(temp) : Stream.empty());
             }.collect(toList());

        or write a utility method and then perform a 'static import' of that utility method.

             return collection.stream()
                .flatMap(str -> nullableToStream(map.get(str)))
                .collect(Collectors.toList());


        CUSTOMER SUBMITTED WORKAROUND :
        I am currently using a static method in a utility class, like so, to reduce some of the pain from this:

        public static <T> Stream<T> nullableToStream(T object) {
        return object != null ? Stream.of(object) : Stream.empty();
        }

        However, each time I use it, I have to add an import, or an 'import static', which is a hassle. Also, different users will of course create different utility methods like this, which results in code portability problems. It would be better to have a single method in the JDK for everybody to use.

        Another possiblity would be to use Optional.ofNullable(T) on the return from Map.get(...), and then to convert the Optional to a stream, but there is currently no Optional.stream() method so this would be much more verbose. (I have just filed a separate RFE for that issue.)


        Attachments

          Issue Links

            Activity

              People

                psandoz Paul Sandoz
                webbuggrp Webbug Group
                Votes:
                0 Vote for this issue
                Watchers:
                4 Start watching this issue

                Dates

                  Created:
                  Updated:
                  Resolved: