Removing HashSet element

53 Views Asked by At

My code currently filling up a HashSet with an object of Class Coin.

private HashSet<Coin> walletcoin;
public Wallet(int WalletSize)
{
    walletcoin = new HashSet<Coin>();
    for(int i = 0; i < WalletSize; i++){
        walletcoin.add(new Coin());
    }
}

This seems to be working fine.

Class Coin:

public class Coin
{
    public Coin(){
    }    
}

The second method is currently trying to remove a Coin object from the HashSet.

public int removeCoin()
{
    Coin toremove = new Coin();
       if(walletcoin.contains(toremove))
       {    
           if(walletcoin.remove(toremove))
           {
               return 1;
           }

       }
       return 7;
}

I have created a new object of Class coin, to try to search against the HashSet. Currently after testing the program skips straight to the final return statement and does not go through the if statements.

How would I be able to modify my code in order for the if statements to correctly work, and remove one of the type Coin elements from the HashSet.

0

There are 0 best solutions below