Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert classic nested for loop with Java 8 streams [duplicate]

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?

like image 311
Francesco Avatar asked Jan 14 '17 19:01

Francesco


1 Answers

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.

like image 140
Dmitry Gorkovets Avatar answered Oct 23 '22 09:10

Dmitry Gorkovets