Create collection of cartesian product of two (and more) lists with Java Lambda

92
June 14, 2022, at 1:40 PM

I'm able to easily achieve this in Scala with something like:

def permute(xs: List[Int], ys: List[Int]) = {
  for {x <- xs; y <- ys} yield (x,y)
}

So if I give it {1, 2}, {3, 4} I return {1, 3}, {1, 4}, {2, 3}, {2, 4}

I was hoping to be able to translate this to java 8 using streams.

I'm having a bit of difficulty and I'd like to be able to extend this out farther as I'd like to be able to generate many permuted test samples from more than two lists.

Will it inevitably be a nested mess even using streams or am I not applying myself enough?

Some additional answers were found after realizing that I was looking for a cartesian product:

How can I make Cartesian product with Java 8 streams?

Answer 1

I'm having difficulty figuring out what you're hoping for, it looks like you're trying to get a Cartesian product? Like, given {1, 2} and {3, 4}, you're expecting {(1, 3), (1, 4), (2, 3), (2, 4)}? (For what it's worth, I don't think that has any relationship to the mathematical definition of permutations, which generally involve different ways of ordering the contents of a single list.)

That could be written

xs.stream()
  .flatMap(x -> ys.stream().map(y -> Pair.of(x, y)))
  .collect(toList());
Answer 2

If you want to avoid repetition, then you are after combinations and not Cartesian product. A way to remove repeated elements is to use a filter after your second stream as follows.

List<Integer> xs = new ArrayList<>(Arrays.asList(1, 2, 3, 4, 5, 6, 7));
List<Integer> ys = new ArrayList<>(Arrays.asList(3, 4, 5, 6, 7));
List<String> combinations =
        xs.stream()
        .flatMap(
                x -> ys.stream()
                .filter( y -> x != y)
                .map(y -> x + ":" + y)
        ).collect(Collectors.toList());
System.out.println(combinations);

This will get you the following:

[1:3, 1:4, 1:5, 1:6, 1:7, 2:3, 2:4, 2:5, 2:6, 2:7, 3:4, 3:5, 3:6, 3:7, 4:3, 4:5, 4:6, 4:7, 5:3, 5:4, 5:6, 5:7, 6:3, 6:4, 6:5, 6:7, 7:3, 7:4, 7:5, 7:6]

I'm coming from the future. That's how I know this. =)

Rent Charter Buses Company
READ ALSO
removeUniqueElementsWithEqualHashesDecrementsSize test nor workin with hastable

removeUniqueElementsWithEqualHashesDecrementsSize test nor workin with hastable

I have one test in this testclass that is not going throughI have implemented how I think it should be written, but I get a message :

89
Conditional code based on Build/flavor - Identical to C# #if DEBUG

Conditional code based on Build/flavor - Identical to C# #if DEBUG

In C# there is a way to include/exclude parts of the code based on build type

85
Java.lang.NullPointerException error while exporting SSL Certificate BurpSuit

Java.lang.NullPointerException error while exporting SSL Certificate BurpSuit

I have burp suite installed on windows 10, I tried to configure the certificate and when I try to download it from the embedded browser I see that it is in cer format and can't be added to firefox or chrome, so I tried to export it using burp option in Der format,...

99