I tried to count occurences of an object in a list, but looks like it's not working.
here is my class Card :
public class Card {
private int value;
private String type;
private String color;
}
and here, i'm trying to set up a deck of 104 cards, containing 2 occurences of each card, but looks like my condition isn't correct :
public static List<Card> InitializeDeck(){
for(int i=0;i<104;i++){
boolean isOk = true;
while (isOk){
int col = (int) (Math.floor(Math.random() * 2) + 1);
if (col == 1) {
color = "Black";
} else {
color = "Red";
}
value = (int) (Math.floor(Math.random() * 14));
while (value == 0) {
value = (int) (Math.floor(Math.random() * 14));
}
int ty = (int) (Math.floor(Math.random() * 4));
switch (ty) {
case 0:
type = "Spade";
break;
case 1:
type = "Heart";
break;
case 2:
type = "Diamond";
break;
case 3:
type = "Club";
break;
}
Card card = new Card(value, type, color);
if(deck.isEmpty() || deck.stream().filter(line -> card.equals(line)).count()<=1){
deck.add(card);
isOk=false;
}
}
}
return deck;
}
I get a deck of 104 cards, but with sometimes 4 occurences of the same card, or even not a single occurence, any tips ?