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?