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?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With