Search code examples
javafor-loopjava-8java-streamnested-loops

Convert classic nested for loop with Java 8 streams


I want to convert the following code using the Java 8 stream API

List<Card> deck = new ArrayList<>();
for (Suit s: Suit.values())
{
    for (Rank r: Rank.values())
    {
        deck .add(new Card(r, s));
    }
}

I came out with this

List<Card> deck = new ArrayList<>();
Arrays.stream(Suit.values())
    .forEach(s -> Arrays.stream(Rank.values())
        .forEach(r -> deck.add(new Card(r, s))));

but I don't like it as it has a side-effect on the list.

Is there another elegant way, producing a list from a stream instead maybe?


Solution

  • Use

    List<Card> cards = Arrays.stream(Suit.values())
                    .flatMap(s -> Arrays.stream(Rank.values()).map(r -> new Card(r, s)))
                    .collect(Collectors.toList());
    

    Actually it is simple Cartesian product. I took an example from Cartesian product of streams in Java 8 as stream (using streams only) and adapted to your case. If you want to make third loop inside you need to use code from this answer.