Null safe Collection as Stream in Java 8

后端 未结 6 1466
天涯浪人
天涯浪人 2020-12-13 02:01

I am looking for method that can make stream of collection, but is null safe. If collection is null, empty stream is returned. Like this:

Utils.nullSafeStrea         


        
相关标签:
6条回答
  • 2020-12-13 02:16

    If downloading the library org.apache.commons.collections4 is not an option, you can just write your own wrapper/convenience method.

    public static <T> Stream<T> asStream(final Collection<T> collection) {
        return collection == null ? ( Stream<T> ) Collections.emptyList().stream() 
                                  : collection.stream();
    }
    

    Or wrapping the collection with Optional.ofNullable

    public static <T> Stream<T> asStream(final Collection<T> collection) {
        return Optional.ofNullable(collection)
                .orElse( Collections.emptySet()).stream();
    }
    
    0 讨论(0)
  • 2020-12-13 02:25

    Not sure if helps, but since Java 9, you can just write:

    Stream.ofNullable(nullableCollection)
          .flatMap(Collection::stream)
    
    0 讨论(0)
  • 2020-12-13 02:29

    You could use Optional :

    Optional.ofNullable(collection).orElse(Collections.emptySet()).stream()...
    

    I chose Collections.emptySet() arbitrarily as the default value in case collection is null. This will result in the stream() method call producing an empty Stream if collection is null.

    Example :

    Collection<Integer> collection = Arrays.asList (1,2,3);
    System.out.println (Optional.ofNullable(collection).orElse(Collections.emptySet()).stream().count ());
    collection = null;
    System.out.println (Optional.ofNullable(collection).orElse(Collections.emptySet()).stream().count ());
    

    Output:

    3
    0
    

    Alternately, as marstran suggested, you can use:

    Optional.ofNullable(collection).map(Collection::stream).orElse(Stream.empty ())...
    
    0 讨论(0)
  • 2020-12-13 02:32

    You can use something like that:

    public static void main(String [] args) {
        List<String> someList = new ArrayList<>();
        asStream(someList).forEach(System.out::println);
    }
    
    public static <T> Stream<T> asStream(final Collection<T> collection) {
        return Optional.ofNullable(collection)
                .map(Collection::stream)
                .orElseGet(Stream::empty);
    }
    
    0 讨论(0)
  • 2020-12-13 02:34

    Your collectionAsStream() method can be simplified to a version even simpler than when using Optional:

    public static <T> Stream<T> collectionAsStream(Collection<T> collection) {
        return collection == null ? Stream.empty() : collection.stream();
    }
    

    Note that in most cases, it's probably better to just test for nullness before building the stream pipeline:

    if (collection != null) {
        collection.stream().filter(...)
    } // else do nothing
    

    What you want seems to be only useful when you need to return the stream (including for flatmapping), or maybe concatenate it with another one.

    0 讨论(0)
  • 2020-12-13 02:35

    You can use org.apache.commons.collections4.CollectionUtils::emptyIfNull function:

      org.apache.commons.collections4.CollectionUtils.emptyIfNull(list).stream().filter(...);
    
    0 讨论(0)
提交回复
热议问题