Class MoreCollectors


  • @GwtCompatible
    public final class MoreCollectors
    extends 
    Collectors not present in java.util.stream.Collectors that are not otherwise associated with a com.google.common type.
    Since:
    21.0
    Author:
    Louis Wasserman
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static <T> <T,​?,​T> onlyElement()
      A collector that takes a stream containing exactly one element and returns that element.
      static <T> <T,​?,​<T>> toOptional()
      A collector that converts a stream of zero or one elements to an Optional.
      • Methods inherited from class java.lang.

        , , , , , , , , , ,
    • Method Detail

      • toOptional

        public static <T> <T,​?,​<T>> toOptional()
        A collector that converts a stream of zero or one elements to an Optional. The returned collector throws an IllegalArgumentException if the stream consists of two or more elements, and a NullPointerException if the stream consists of exactly one element, which is null.
      • onlyElement

        public static <T> <T,​?,​T> onlyElement()
        A collector that takes a stream containing exactly one element and returns that element. The returned collector throws an IllegalArgumentException if the stream consists of two or more elements, and a NoSuchElementException if the stream is empty.