Java 8 Stream, getting head and tail

后端 未结 9 2071
太阳男子
太阳男子 2020-11-30 05:40

Java 8 introduced a Stream class that resembles Scala\'s Stream, a powerful lazy construct using which it is possible to do something like this very concisely:



        
9条回答
  •  春和景丽
    2020-11-30 06:35

    There are many interesting suggestions provided here, but if someone needs a solution without dependencies to third party libraries I came up with this:

        import java.util.AbstractMap;
        import java.util.Optional;
        import java.util.Spliterators;
        import java.util.stream.StreamSupport;
    
        /**
         * Splits a stream in the head element and a tail stream.
         * Parallel streams are not supported.
         * 
         * @param stream Stream to split.
         * @param  Type of the input stream.
         * @return A map entry where {@link Map.Entry#getKey()} contains an
         *    optional with the first element (head) of the original stream
         *    and {@link Map.Entry#getValue()} the tail of the original stream.
         * @throws IllegalArgumentException for parallel streams.
         */
        public static  Map.Entry, Stream> headAndTail(final Stream stream) {
            if (stream.isParallel()) {
                throw new IllegalArgumentException("parallel streams are not supported");
            }
            final Iterator iterator = stream.iterator();
            return new AbstractMap.SimpleImmutableEntry<>(
                    iterator.hasNext() ? Optional.of(iterator.next()) : Optional.empty(),
                    StreamSupport.stream(Spliterators.spliteratorUnknownSize(iterator, 0), false)
            );
        }
    

提交回复
热议问题