0

I created a HashSet of Console objects which use 2 strings. One is for the company name for the console, the other is the console name. I have no issues with most of the basic methods (I know how to add to a HashSet, clear a HashSet etc...), but I am having issues with removing a single object, and with checking if the HashSet contains a certain object. I have included the relevant pieces of code for the HashSet class

   import java.util.HashSet;

    public class testHashSet
    {
    private HashSet <Console> testSet;

    /**
     * Constructor
     */
    public testHashSet()
    {
        testSet = new HashSet <Console> () ;
        setTestSet();
    }

    public void setTestSet(){
        testSet.add(new Console("Nintendo" , "Wii U"));
        testSet.add(new Console("Sony" , "PS4"));
        testSet.add(new Console("XBox" , "Microsoft"));
    }

    /**
     * Method to remove object from HashSet
     */
    public void removeData(){
        Console d = new Console("Sony" , "PS4");
        testSet.remove(d);
        printHashSet();
    }

    /**
     * Method to check for specific object in HashSet
     */
    public void checkForItem(String anyCompany, String anyConsole){
        boolean exist = testSet.contains(new Console (anyCompany, anyConsole));
        System.out.println(exist);
    }

Edit: Here is the source code for the Console class:

public class Console
{
private String companyName;
private String consoleName;

/**
 * Constructor for objects of class Console
 */
public Console(String anyCompany, String anyConsole)
{
    companyName = companyName;
    consoleName = anyConsole;
}

public void printConsoleInfo(){
    System.out.println(consoleName + " is a " + companyName + " console.");
}
}
dimo414
  • 47,227
  • 18
  • 148
  • 244
  • 4
    Your Console class probably doesn't override `hashCode` and `equals`. – Misha Apr 01 '15 at 01:29
  • Indeed - without seeing the code for `Console`, we can't tell what's wrong. But yes, it's almost certainly not overriding `hashCode` and `equals` appropriately. – Jon Skeet Apr 01 '15 at 01:30
  • Ok, I just added the code. –  Apr 01 '15 at 01:33
  • possible duplicate of [Understanding the workings of equals and hashCode in a HashMap](http://stackoverflow.com/questions/1894377/understanding-the-workings-of-equals-and-hashcode-in-a-hashmap) – dimo414 Apr 01 '15 at 02:25
  • @dimo414 I'm afraid I don't see how that is really relevant to this. –  Apr 01 '15 at 02:33

1 Answers1

2

You did not override hashCode() and equals() in your Console class.

Therefore, you end up with java's default implementation, which considers two instances of Console (and you are creating new instances in your methods) to be different even if you pass the same strings to the constructor.

To solve your issue, you need to "help" java identify equal objects by implementing a custom equals() method in your Console class. To comply with the general contract (and due to the way a HashSet tries to find elements) you will then also need to implement a custom hashCode() method as well.

A basic implementation could e.g. be

@Override
public int hashCode() {
    final int prime = 31;
    int result = 1;
    result = prime * result
            + ((companyName == null) ? 0 : companyName.hashCode());
    result = prime * result
            + ((consoleName == null) ? 0 : consoleName.hashCode());
    return result;
}

@Override
public boolean equals(Object obj) {
    if (this == obj)
        return true;
    if (obj == null)
        return false;
    if (getClass() != obj.getClass())
        return false;
    Console other = (Console) obj;
    if (companyName == null) {
        if (other.companyName != null)
            return false;
    } else if (!companyName.equals(other.companyName))
        return false;
    if (consoleName == null) {
        if (other.consoleName != null)
            return false;
    } else if (!consoleName.equals(other.consoleName))
        return false;
    return true;
}

(as generated by eclipse)

Bonus: If it's not a typo in the question, you'll also want to change the assignment in your constructor from

companyName = companyName;

to

companyName = anyCompany;
Marvin
  • 13,325
  • 3
  • 51
  • 57