Zipping streams using JDK8 with lambda (java.util.stream.Streams.zip)

前端 未结 14 1303
旧巷少年郎
旧巷少年郎 2020-11-21 23:24

In JDK 8 with lambda b93 there was a class java.util.stream.Streams.zip in b93 which could be used to zip streams (this is illustrated in the tutorial Exploring Java8 Lambda

相关标签:
14条回答
  • 2020-11-21 23:33

    This is great. I had to zip two streams into a Map with one stream being the key and other being the value

    Stream<String> streamA = Stream.of("A", "B", "C");
    Stream<String> streamB  = Stream.of("Apple", "Banana", "Carrot", "Doughnut");    
    final Stream<Map.Entry<String, String>> s = StreamUtils.zip(streamA,
                        streamB,
                        (a, b) -> {
                            final Map.Entry<String, String> entry = new AbstractMap.SimpleEntry<String, String>(a, b);
                            return entry;
                        });
    
    System.out.println(s.collect(Collectors.toMap(e -> e.getKey(), e -> e.getValue())));
    

    Output: {A=Apple, B=Banana, C=Carrot}

    0 讨论(0)
  • 2020-11-21 23:40
    public class Tuple<S,T> {
        private final S object1;
        private final T object2;
    
        public Tuple(S object1, T object2) {
            this.object1 = object1;
            this.object2 = object2;
        }
    
        public S getObject1() {
            return object1;
        }
    
        public T getObject2() {
            return object2;
        }
    }
    
    
    public class StreamUtils {
    
        private StreamUtils() {
        }
    
        public static <T> Stream<Tuple<Integer,T>> zipWithIndex(Stream<T> stream) {
            Stream<Integer> integerStream = IntStream.range(0, Integer.MAX_VALUE).boxed();
            Iterator<Integer> integerIterator = integerStream.iterator();
            return stream.map(x -> new Tuple<>(integerIterator.next(), x));
        }
    }
    
    0 讨论(0)
  • 2020-11-21 23:43

    If you have Guava in your project, you can use the Streams.zip method (was added in Guava 21):

    Returns a stream in which each element is the result of passing the corresponding element of each of streamA and streamB to function. The resulting stream will only be as long as the shorter of the two input streams; if one stream is longer, its extra elements will be ignored. The resulting stream is not efficiently splittable. This may harm parallel performance.

     public class Streams {
         ...
    
         public static <A, B, R> Stream<R> zip(Stream<A> streamA,
                 Stream<B> streamB, BiFunction<? super A, ? super B, R> function) {
             ...
         }
     }
    
    0 讨论(0)
  • 2020-11-21 23:43

    The Lazy-Seq library provides zip functionality.

    https://github.com/nurkiewicz/LazySeq

    This library is heavily inspired by scala.collection.immutable.Stream and aims to provide immutable, thread-safe and easy to use lazy sequence implementation, possibly infinite.

    0 讨论(0)
  • 2020-11-21 23:44

    Would this work for you? It's a short function, which lazily evaluates over the streams it's zipping, so you can supply it with infinite streams (it doesn't need to take the size of the streams being zipped).

    If the streams are finite it stops as soon as one of the streams runs out of elements.

    import java.util.Objects;
    import java.util.function.BiFunction;
    import java.util.stream.Stream;
    
    class StreamUtils {
        static <ARG1, ARG2, RESULT> Stream<RESULT> zip(
                Stream<ARG1> s1,
                Stream<ARG2> s2,
                BiFunction<ARG1, ARG2, RESULT> combiner) {
            final var i2 = s2.iterator();
            return s1.map(x1 -> i2.hasNext() ? combiner.apply(x1, i2.next()) : null)
                    .takeWhile(Objects::nonNull);
        }
    }
    

    Here is some unit test code (much longer than the code itself!)

    import org.junit.jupiter.api.Test;
    import org.junit.jupiter.params.ParameterizedTest;
    import org.junit.jupiter.params.provider.Arguments;
    import org.junit.jupiter.params.provider.MethodSource;
    
    import java.util.List;
    import java.util.concurrent.atomic.AtomicInteger;
    import java.util.function.BiFunction;
    import java.util.stream.Collectors;
    import java.util.stream.Stream;
    
    import static org.junit.jupiter.api.Assertions.assertEquals;
    
    class StreamUtilsTest {
        @ParameterizedTest
        @MethodSource("shouldZipTestCases")
        <ARG1, ARG2, RESULT>
        void shouldZip(
                String testName,
                Stream<ARG1> s1,
                Stream<ARG2> s2,
                BiFunction<ARG1, ARG2, RESULT> combiner,
                Stream<RESULT> expected) {
            var actual = StreamUtils.zip(s1, s2, combiner);
    
            assertEquals(
                    expected.collect(Collectors.toList()),
                    actual.collect(Collectors.toList()),
                    testName);
        }
    
        private static Stream<Arguments> shouldZipTestCases() {
            return Stream.of(
                    Arguments.of(
                            "Two empty streams",
                            Stream.empty(),
                            Stream.empty(),
                            (BiFunction<Object, Object, Object>) StreamUtilsTest::combine,
                            Stream.empty()),
                    Arguments.of(
                            "One singleton and one empty stream",
                            Stream.of(1),
                            Stream.empty(),
                            (BiFunction<Object, Object, Object>) StreamUtilsTest::combine,
                            Stream.empty()),
                    Arguments.of(
                            "One empty and one singleton stream",
                            Stream.empty(),
                            Stream.of(1),
                            (BiFunction<Object, Object, Object>) StreamUtilsTest::combine,
                            Stream.empty()),
                    Arguments.of(
                            "Two singleton streams",
                            Stream.of("blah"),
                            Stream.of(1),
                            (BiFunction<Object, Object, Object>) StreamUtilsTest::combine,
                            Stream.of(pair("blah", 1))),
                    Arguments.of(
                            "One singleton, one multiple stream",
                            Stream.of("blob"),
                            Stream.of(2, 3),
                            (BiFunction<Object, Object, Object>) StreamUtilsTest::combine,
                            Stream.of(pair("blob", 2))),
                    Arguments.of(
                            "One multiple, one singleton stream",
                            Stream.of("foo", "bar"),
                            Stream.of(4),
                            (BiFunction<Object, Object, Object>) StreamUtilsTest::combine,
                            Stream.of(pair("foo", 4))),
                    Arguments.of(
                            "Two multiple streams",
                            Stream.of("nine", "eleven"),
                            Stream.of(10, 12),
                            (BiFunction<Object, Object, Object>) StreamUtilsTest::combine,
                            Stream.of(pair("nine", 10), pair("eleven", 12)))
            );
        }
    
        private static List<Object> pair(Object o1, Object o2) {
            return List.of(o1, o2);
        }
    
        static private <T1, T2> List<Object> combine(T1 o1, T2 o2) {
            return List.of(o1, o2);
        }
    
        @Test
        void shouldLazilyEvaluateInZip() {
            final var a = new AtomicInteger();
            final var b = new AtomicInteger();
            final var zipped = StreamUtils.zip(
                    Stream.generate(a::incrementAndGet),
                    Stream.generate(b::decrementAndGet),
                    (xa, xb) -> xb + 3 * xa);
    
            assertEquals(0, a.get(), "Should not have evaluated a at start");
            assertEquals(0, b.get(), "Should not have evaluated b at start");
    
            final var takeTwo = zipped.limit(2);
    
            assertEquals(0, a.get(), "Should not have evaluated a at take");
            assertEquals(0, b.get(), "Should not have evaluated b at take");
    
            final var list = takeTwo.collect(Collectors.toList());
    
            assertEquals(2, a.get(), "Should have evaluated a after collect");
            assertEquals(-2, b.get(), "Should have evaluated b after collect");
            assertEquals(List.of(2, 4), list);
        }
    }
    
    0 讨论(0)
  • 2020-11-21 23:46

    I needed this as well so I just took the source code from b93 and put it in a "util" class. I had to modify it slightly to work with the current API.

    For reference here's the working code (take it at your own risk...):

    public static<A, B, C> Stream<C> zip(Stream<? extends A> a,
                                         Stream<? extends B> b,
                                         BiFunction<? super A, ? super B, ? extends C> zipper) {
        Objects.requireNonNull(zipper);
        Spliterator<? extends A> aSpliterator = Objects.requireNonNull(a).spliterator();
        Spliterator<? extends B> bSpliterator = Objects.requireNonNull(b).spliterator();
    
        // Zipping looses DISTINCT and SORTED characteristics
        int characteristics = aSpliterator.characteristics() & bSpliterator.characteristics() &
                ~(Spliterator.DISTINCT | Spliterator.SORTED);
    
        long zipSize = ((characteristics & Spliterator.SIZED) != 0)
                ? Math.min(aSpliterator.getExactSizeIfKnown(), bSpliterator.getExactSizeIfKnown())
                : -1;
    
        Iterator<A> aIterator = Spliterators.iterator(aSpliterator);
        Iterator<B> bIterator = Spliterators.iterator(bSpliterator);
        Iterator<C> cIterator = new Iterator<C>() {
            @Override
            public boolean hasNext() {
                return aIterator.hasNext() && bIterator.hasNext();
            }
    
            @Override
            public C next() {
                return zipper.apply(aIterator.next(), bIterator.next());
            }
        };
    
        Spliterator<C> split = Spliterators.spliterator(cIterator, zipSize, characteristics);
        return (a.isParallel() || b.isParallel())
               ? StreamSupport.stream(split, true)
               : StreamSupport.stream(split, false);
    }
    
    0 讨论(0)
提交回复
热议问题