0

I have a hashmap which key is an object of my inner class "Key".

My problem is that when I use get(key) it never gives anything back. Since get works with equals I have overwritten equals in my Key class, so it should work for the get method, but apparently it does not.

Any suggestions?

CODE:

public class Infrastruktur 
{
    private Zuechter online;
    private HashMap<Key,Zuechter> zuechter;

    Infrastruktur()
    {
        zuechter = new HashMap<Key,Zuechter>();
    }

    }

    public void login(String name, String passwort) 
    {
        Key hashMapKey = new Key(name, passwort);
        if(this.zuechter.get(hashMapKey) != null)
            this.online = this.zuechter.get(hashMapKey);
    }

    public void register(String name, String passwort)
    {
        if(name != null && passwort != null)
        {
            this.zuechter.put(new Key(name,passwort),new Zuechter());
            login(name, passwort);
        }
    }

    public void logOut()
    {
        this.online = null;
    }

    public Zuechter getOnline() {
        return this.online;
    }

    private class Key 
    {
        String name;
        String passwort;

        Key(String name, String passwort) 
        {
            this.name = name;
            this.passwort = passwort;
        }

        @Override
        public boolean equals(Object o)
        {
            if (o == null) return false;
            if (o == this) return true;
            if (!(o instanceof Key)) return false;
            Key key = (Key)o;
            if(this.name.equals(key.name) && this.passwort.equals(key.passwort)) return true;
            return false;
        }
    }

    /* Testing */
    public static void main(String[] args)
    {
        Infrastruktur inf = new Infrastruktur();
        inf.register("Jakob", "passwort");
        inf.logOut();
        inf.login("Jakob", "passwort");
        System.out.println(inf.getOnline().test());
    }
}

If I run the class this is the output I get:

not found
not found
Exception in thread "main" java.lang.NullPointerException
        at Infrastruktur.main(Infrastruktur.java:105)
Jakob Abfalter
  • 4,980
  • 17
  • 54
  • 94

3 Answers3

2

You should also implement hashCode() for your Key class. An example implementation could be:

@Override
public int hashCode() {
    final int prime = 31;
    int result = 1;
    result = prime * result + name.hashCode();
    result = prime * result + passwort.hashCode();
    return result;
}
Mureinik
  • 297,002
  • 52
  • 306
  • 350
2

Use Eclipse to generate the hashCode method of your class. In any Map scenario, Java hashes the key value to allow 0(1) read access.

It simply hashes to jump to a reference if found. All Java IDEs have a Generate hashCode and equals option. A simple example, with null checks omitted.

@Override
  public int hashCode() {
    int hash = 3;
    hash = 7 * hash + this.name.hashCode();
    hash = 7 * hash + this.passwort.hashCode();
    return hash;
  }
flavian
  • 28,161
  • 11
  • 65
  • 105
1

You must override hashCode() in every class that overrides equals(). Failure to do so will result in a violation of the general contract for Object.hashCode(), which will prevent your class from functioning properly in conjunction with all hash-based collections, including HashMap, HashSet, and Hashtable.

from Effective Java, by Joshua Bloch

tl;dr either generate hashCode() manually,

@Override
  public int hashCode() {
    int hash = 31;
    hash = 29 * hash + Objects.hashCode(name);
    hash = 29 * hash + Objects.hashCode(passwort);
    return hash;
  }

use IDE hashCode generation, or just use generic (albeit slower)

@Override
  public int hashCode() {
    return Objects.hash( name, passwort );
  }

... you can even write a generic hashCode() for any class using reflection (very slow, but good as placeholder)

btw, omitting null checks in hashCode() for mutable or immutable objects with null as a valid field value is one of the easiest ways to introduce bugs into code - that's exactly why either explicit check or Objects.hashCode() is needed.

Community
  • 1
  • 1