I want to convert the following code using the Java 8 thread API
List<Card> deck = new ArrayList<>();
for (Suit s: Suit.values())
{
for (Rank r: Rank.values())
{
deck .add(new Card(r, s));
}
}
I went 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 because it has a side effect on the list.
Is there another elegant way that creates a list from a stream, maybe?
source
share