You should create a named constructor (from Effective Java J.Bloch) for your Card class and override equals and hashCode for it like this:
class Card {
private String valueAsString;
/*your code here*/
public static Card of(String valueAsString) {
Card card = /*create your card here*/
card.valueAsString = valueAsString;
return card;
}
@Override
public boolean equals(Object other) {
/*another part of equals*/
return Objects.equals(valueAsString, other.valueAsString);
}
@Override
public int hashCode() {
return Objects.hash(valueAsString);
}
}
With that named constructor you can just do the following thing:
if (player1Hand.contains(Card.of(split[2]))) {
/*do something*/
}
The explanation: as soon as ArrayList uses an equals method to compare elements in the array you need to define it properly. Besides, a named constructor helps you to create a placeholder object based on value of split[2]. With this two steps you will be able to use contains and it will work as you want to. Why should you override a hashCode too? You should do it, because there is an informal rule that you should always override equals and hashCode like a pair, because they have to do it's stuff with the same set of fields.
Map<String,Card>instead ofArrayList<Card>and usemap.containsKey(split[2])player1hand.contains(new Card(split[2])). It is possible that you need to overloadequalsfor this approach