我应该使用Java 8 Streams Api来组合两个Collection吗? [英] Should I use Java 8 Streams Api to combine two Collections?

查看:43
本文介绍了我应该使用Java 8 Streams Api来组合两个Collection吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在这种情况下,似乎Java 8 Streams API会有所帮助,但我不确定如何实现.

I have this situation where it seems that the Java 8 Streams API would be helpful, but I'm not fully sure how it could be.

从两个具有不同元素类型的集合中,我要构建第三个集合,其元素都是两个集合中所有可能的元素 Pairs .基本上:

From two Collections with distinct element types, I want to build a third collection whose elements are all the possible Pairs of elements from both collections. Basically:

两种不同的元素类型...

The two distinct element types...

public class A {}
public class B {}

一对" As和Bs.

public class Pair {
   private A a;
   private B b;

   public Pair(A a, B b){
     this a = a;
     this b = b;
   }
}

使用旧式java.util.Collection API制成的"组合":

The "combination" made using old-style java.util.Collection API:

 public Collection<Pair> combine(Collection<A> as, Collection<B> bs){
    Collection<Pair> pairs = new ArrayList();
    foreach(A a: as){
      foreach(B b: bs){
          Pair pair = new Pair(a,b);
          pairs.add(pair);
      }
    }
    return pairs;
 }

结果对对中的排序并不重要.因此,可以创建Pair的每个实例,并将其并行添加到结果集合中.我怎么能做到这一点?

The ordering in the resulting pairs collection is not important. So, every instance of Pair could be created and added to the resulting collection in parallel. How could I achieve this?

我自己能弄清楚的最好方法是使用foreach的Streams版本:

The best I could figure out by myself was to use the Streams version of foreach:

as.foreach(
  a -> {
    bs.foreach(
      b -> {
          Pair pair = new Pair(a,b);
          pairs.add(pair);
      }
  }
);

为简化起见,此示例变得无关紧要. Pair类是将两个元素处理为第三个元素(即java.util.function.BiFunction)的示例,将它们添加到Collection只是可变归约的示例.

This example was made trivial for the sake of simplification. The class Pair is an example of processing two elements into a third one (that is, a java.util.function.BiFunction), and adding them to a Collection is just an example of a mutable reduction.

有没有更优雅的方式做到这一点?或更可取的是,在效率方面以更有利可图的方式?像

Is there a more elegant way to do that? Or preferable, in a more profitable way in regards to efficiency? Something like

BiFunction<A,B,Pair> combinator = Pair::new; //or any other function f(a,b)=c;

Stream<Pair> pairStream = 
  Streams.unknownElegantMethod(as.stream(), bs.stream(), combinator);

推荐答案

我希望我没有任何愚蠢的错字,但基本上您可以做的是:

I hope I don't have any silly typos, but basically what you can do is :

List<Pair> list = as
                  .stream()
                  .flatMap(a -> bs.stream().map (b -> new Pair(a,b)))
                  .collect (Collectors.toList());

  1. 首先,您从as创建一个Stream<A>.
  2. 对于每个a实例
    2.1创建bs
    Stream<B> 2.2将每个b映射到一对(a,b)
  3. 将所有对放到单个流中.
  4. 最后我将它们收集到一个列表中,尽管您可以选择其他集合.
  1. First you create a Stream<A> from as.
  2. For each a instance
    2.1 Create a Stream<B> of bs
    2.2 Map each b to a pair of (a,b)
  3. Flatten all the pairs to a single Stream.
  4. Finally I collected them to a List, though you can choose other collections.

这篇关于我应该使用Java 8 Streams Api来组合两个Collection吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆