有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

使用JDK8和lambda(java.util.stream.streams.zip)对流进行函数式编程压缩

在带有lambda b93的JDK 8中,有一个类java.util.stream.Streams.zip in b93,可以用来压缩流(这在教程Exploring Java8 Lambdas. Part 1 by Dhananjay Nene中有说明)。此功能:

Creates a lazy and sequential combined Stream whose elements are the result of combining the elements of two streams.

然而,在b98中,这种情况已经消失。实际上Streams类在java.util.stream in b98中甚至是不可访问的

此功能是否已移动,如果已移动,如何使用b98简洁地压缩流

我想到的应用程序是in this java implementation of Shen,在那里我替换了

  • static <T> boolean every(Collection<T> c1, Collection<T> c2, BiPredicate<T, T> pred)
  • static <T> T find(Collection<T> c1, Collection<T> c2, BiPredicate<T, T> pred)

代码相当冗长的函数(不使用b98中的功能)


共 (4) 个答案

  1. # 1 楼答案

    您提到的类的方法已被移动到Stream接口本身,以支持默认方法。但是zip方法似乎已被删除。可能是因为不清楚不同大小的流的默认行为应该是什么。但实现期望的行为是直截了当的:

    static <T> boolean every(
      Collection<T> c1, Collection<T> c2, BiPredicate<T, T> pred) {
        Iterator<T> it=c2.iterator();
        return c1.stream().allMatch(x->!it.hasNext()||pred.test(x, it.next()));
    }
    static <T> T find(Collection<T> c1, Collection<T> c2, BiPredicate<T, T> pred) {
        Iterator<T> it=c2.iterator();
        return c1.stream().filter(x->it.hasNext()&&pred.test(x, it.next()))
          .findFirst().orElse(null);
    }
    
  2. # 2 楼答案

    使用JDK8和lambda(gist)压缩两个流

    public static <A, B, C> Stream<C> zip(Stream<A> streamA, Stream<B> streamB, BiFunction<A, B, C> zipper) {
        final Iterator<A> iteratorA = streamA.iterator();
        final Iterator<B> iteratorB = streamB.iterator();
        final Iterator<C> iteratorC = new Iterator<C>() {
            @Override
            public boolean hasNext() {
                return iteratorA.hasNext() && iteratorB.hasNext();
            }
    
            @Override
            public C next() {
                return zipper.apply(iteratorA.next(), iteratorB.next());
            }
        };
        final boolean parallel = streamA.isParallel() || streamB.isParallel();
        return iteratorToFiniteStream(iteratorC, parallel);
    }
    
    public static <T> Stream<T> iteratorToFiniteStream(Iterator<T> iterator, boolean parallel) {
        final Iterable<T> iterable = () -> iterator;
        return StreamSupport.stream(iterable.spliterator(), parallel);
    }
    
  3. # 3 楼答案

    zip是protonpack library提供的函数之一

    Stream<String> streamA = Stream.of("A", "B", "C");
    Stream<String> streamB  = Stream.of("Apple", "Banana", "Carrot", "Doughnut");
    
    List<String> zipped = StreamUtils.zip(streamA,
                                          streamB,
                                          (a, b) -> a + " is for " + b)
                                     .collect(Collectors.toList());
    
    assertThat(zipped,
               contains("A is for Apple", "B is for Banana", "C is for Carrot"));
    
  4. # 4 楼答案

    如果项目中有番石榴,可以使用Streams.zip方法(在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) {
             ...
         }
     }