Class MoreCollectors


  • @GwtCompatible
    public final class MoreCollectors
    extends java.lang.Object
    Collectors not present in java.util.stream.Collectors that are not otherwise associated with a com.google.common type.
    Since:
    21.0
    • Nested Class Summary

      Nested Classes 
      Modifier and Type Class Description
      private static class  MoreCollectors.ToOptionalState
      This atrocity is here to let us report several of the elements in the stream if there were more than one, not just two.
    • Field Summary

      Fields 
      Modifier and Type Field Description
      private static java.lang.Object NULL_PLACEHOLDER  
      private static java.util.stream.Collector<java.lang.Object,​?,​java.lang.Object> ONLY_ELEMENT  
      private static java.util.stream.Collector<java.lang.Object,​?,​java.util.Optional<java.lang.Object>> TO_OPTIONAL  
    • Constructor Summary

      Constructors 
      Modifier Constructor Description
      private MoreCollectors()  
    • Method Summary

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

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • TO_OPTIONAL

        private static final java.util.stream.Collector<java.lang.Object,​?,​java.util.Optional<java.lang.Object>> TO_OPTIONAL
      • NULL_PLACEHOLDER

        private static final java.lang.Object NULL_PLACEHOLDER
      • ONLY_ELEMENT

        private static final java.util.stream.Collector<java.lang.Object,​?,​java.lang.Object> ONLY_ELEMENT
    • Constructor Detail

      • MoreCollectors

        private MoreCollectors()
    • Method Detail

      • toOptional

        public static <T> java.util.stream.Collector<T,​?,​java.util.Optional<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> java.util.stream.Collector<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.