0

I am creating a game of set and in the game I have an object called cards which takes in the attributes of each card. After each card is created I am sending it to an ArrayList which is then put into a LinkedHashSet, the ArrayList is then cleared and then the LinkedHashSet is added back into the ArrayList. After the for loop is done, I add each index of the ArrayList to a queue. I was under the impression that a LinkedHashSet doesn't allow duplicates however I am still getting them. I have tried many different ways using two queues, or two ArrayLists, or a queue and an ArrayList. The most recent way I tried was with the LinkedHashSet and now I am really puzzeled. Attached is my for loop that determines each card attribute and the Cards class which creates each card. Any help would be greatly appreciated.

For loop to make card Attributes

for(int i = queue.size(); i < 81; i++) {
        randomColor = ThreadLocalRandom.current().nextInt(0, 2+1);
        randomNum = ThreadLocalRandom.current().nextInt(0, 2+1);
        randomPattern = ThreadLocalRandom.current().nextInt(0, 2+1);
        randomShape = ThreadLocalRandom.current().nextInt(0, 2+1);
        cardColor = color[randomColor];
        cardShape = shape[randomShape];
        num = number[randomNum];
        cardPattern = pattern[randomPattern];
        cardInfo[0] = num;
        cardInfo[1] = cardColor;
        cardInfo[2] = cardPattern;
        cardInfo[3] = cardShape;
        info = new Cards(num, cardColor, cardPattern, cardShape);
        cardHolder.add(info);
        hashSet.addAll(cardHolder);
        cardHolder.clear();
        cardHolder.addAll(hashSet);
    }

Cards Class:

public class Cards {
private int number;
private String color;
private String pattern;
private String shape;

public Cards(int number, String color, String pattern, String shape) {
    this.number = number;
    this.color = color;
    this.pattern = pattern;
    this.shape = shape;
}
public int getNumber() {
    return number;
}
public String getShape() {
    return shape;
}
public String getColor() {
    return color;
}
public String getPattern() {
    return pattern;
}

public void setNumber(int number) {
    this.number = number;
}
public void setColor(String color) {
    this.color = color;
}
public void setPattern(String pattern) {
    this.pattern = pattern;
}
public void setShape(String shape) {
    this.shape = shape;
}
public String toString() {
    return number + "," + color + "," + pattern + "," + shape;
}

}

Matt
  • 12,848
  • 2
  • 31
  • 53
apex
  • 37
  • 7
  • 4
    Have you implemented `hashCode()` and `equals()` correctly? – Henry Twist Jul 08 '21 at 18:12
  • im still a little confused as to why we would need to do that. Other solutions using LinkedHashSets don't mention to implment hashCode() and equals() – apex Jul 08 '21 at 18:20
  • Can you give an example? It would be impossible for a collection to disallow duplicates if it doesn't know how compare equality. – Henry Twist Jul 08 '21 at 18:34
  • well what would i return as the hashcode value? Can I return this.number, this.shape, etc all at once? And for equals i get I need to tell it when its equal to what but where do i write this method? In my Cards class? And do i need to explicitly need to call .hashCode() and .equals()? – apex Jul 08 '21 at 18:42
  • I meant an example of other solutions that don't require their implementation? However in terms of implementation, the link I provided shows a great example, is there anything in particular you don't understand about it? – Henry Twist Jul 08 '21 at 19:01
  • It might be best to do some research, then come back if you have a specific question. An article like [this](https://www.baeldung.com/java-equals-hashcode-contracts) would give you more detail if you search for it. – Henry Twist Jul 08 '21 at 19:03

0 Answers0