Skip to content

Instantly share code, notes, and snippets.

@rokon12
Created October 5, 2017 19:26
Show Gist options
  • Save rokon12/1b3b5cb3f8a74beb16a95c7be0c35678 to your computer and use it in GitHub Desktop.
Save rokon12/1b3b5cb3f8a74beb16a95c7be0c35678 to your computer and use it in GitHub Desktop.
package com.bazlur;
import java.util.EnumSet;
import java.util.Set;
import java.util.List;
import java.util.function.BiFunction;
import java.util.stream.Collectors;
import java.util.stream.Stream;
public class Card {
enum Suit {
CLUBS,
SPADES,
HEARTS,
DIAMONDS
}
enum Rank {
ACE,
TWO,
THREE,
FOUR,
FIVE,
SIX,
SEVEN,
EIGHT,
NIGHT,
TEN,
JACK,
QUEEN,
KING
}
private Suit suit;
private Rank rank;
public Card(Suit suit, Rank rank) {
this.suit = suit;
this.rank = rank;
}
@Override
public String toString() {
return suit + " - " + rank;
}
private static <A, B, C> Stream<C> cartesianProduct(Set<A> setA, Set<B> setB,
BiFunction<A, B, C> function) {
return setA.stream()
.flatMap(first ->
setB.stream()
.map(second -> function.apply(first, second)));
}
public static List<Card> allCards() {
return cartesianProduct(
EnumSet.allOf(Suit.class),
EnumSet.allOf(Rank.class),
Card::new)
.collect(Collectors.toList());
}
public static void main(String[] args) {
Card.allCards()
.forEach(System.out::println);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment