15

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.

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 {}

A "pair" of As and Bs.

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

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

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;
 }

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?

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);
      }
  }
);

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);
3
  • 1
    Not related to the question, but instead of (a,b) -> {return new Pair(a,b)}; use Pair::new. And for the question, stream are usable once. And you Java code shows that you are using a stream more than one time (the one on bs`). Commented Oct 3, 2014 at 15:02
  • Nice point about Pair:new, I've updated the question. Second point is mistaken, as bs is a Collection, and not a stream.
    – bruno
    Commented Oct 3, 2014 at 20:16
  • No, I understood. What I meant is that when you used up a stream, you can't use it anymore. So the solution can't or will never be something like Stream<Pair> pairStream = Streams.unknownElegantMethod(as.stream(), bs.stream(), combinator); if, and only if, you want all possible pairs of (A, B). The use of flatMap (as in the answer), or foreach (as in your code), is the only solution. Commented Oct 3, 2014 at 23:00

2 Answers 2

24

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. 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.
3
  • 2
    typo: bs, not b, is the name of the collection of b's. SE won't let me make this useful edit :( Commented Jul 30, 2014 at 22:54
  • When compiling this, I get 'Type mismatch: cannot convert from List<Object> to List<Pair>'. flatMap doesn't seem to be getting the correct type from the lambda. I fixed it with List<Pair> list = as.stream().flatMap(a -> bs.stream().map (b -> new Pair(a,b)).collect(Collectors.toList()).stream()).collect(Collectors.toList()); but the fix seems kind of adhoc as it converts the stream into a collection then back to a stream again.
    – Skul
    Commented Aug 11, 2015 at 10:27
  • 1
    this is called a 'cartesian' or 'cross' join Commented Oct 22, 2016 at 15:37
4

If you're open to using a third-party library, you could use Eclipse Collections Sets.cartesianProduct(). This would require that your a's and b's are both Sets. Eclipse Collections has a Pair type built-in, so you wouldn't need to create it.

public class A {}
public class B {}

public List<Pair<A, B>> combine(Set<A> as, Set<B> bs)
{
    return Sets.cartesianProduct(as, bs).toList();
}

If your a's and b's are not Sets, then you could use a CollectionAdapter flatCollect and collect, which are equivalent to flatMap and map on Stream.

public Collection<Pair<A, B>> combine(Collection<A> as, Collection<B> bs)
{
    MutableCollection<B> adaptB = CollectionAdapter.adapt(bs);
    return CollectionAdapter.adapt(as)
            .flatCollect(a -> adaptB.asLazy().collect(b -> Tuples.pair(a, b)));
}

Another possible option using Stream would be to define your own Collector for cartesianProduct. This is more complex than the other Stream solution, and would only be useful if you used cartesianProduct a few times in your code.

List<Pair<A, B>> pairs = as.stream().collect(cartesianProduct(bs));

public static <T1, T2> Collector<T1, ?, List<Pair<T1, T2>>> 
    cartesianProduct(Collection<T2> other)
{
    return Collector.of(
            ArrayList::new,
            (list, a) -> list.addAll(
                other.stream().map(b -> new Pair(a, b))).collect(Collectors.toList())),
            (list1, list2) ->
            {
                list1.addAll(list2);
                return list1;
            },
            Collector.Characteristics.UNORDERED
    );
}

Note: I am a committer for Eclipse Collections.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.