1

When java calls the get method from hashmap does java perform a equals() comparison?

I've read that it does but with the errors I'm getting, it seems like its doing a == comparison.

public class UniversalFiniteStateAutomaton {

State currentState;
State initialState;
State trapState;

public UniversalFiniteStateAutomaton(ArrayList<String> finalStates,
        ArrayList<String> transitions) {
    String statesAndTransitions[];
    Map<Symbol<E>, State> createdStates = new HashMap<Symbol<E>, State>();
    for (String s : transitions) {
        // Replace the stuff that doesn't matter
        s = s.replaceAll("[()]", "");
        // Split the transition into states and transitions
        statesAndTransitions = s.split("\\s+");

        // Create the state if its not already created
        if (finalStates.contains(new Symbol(statesAndTransitions[0]))) {
            if (!createdStates.containsKey((new Symbol(statesAndTransitions[0])))) {
                createdStates.put(new Symbol(statesAndTransitions[0]),
                        new FinalState(this));
                System.out.println("Created one symb " + new Symbol(statesAndTransitions));
            }
        } else {
            if (!createdStates.containsKey(new Symbol(statesAndTransitions[0]))) {
                createdStates.put(new Symbol(statesAndTransitions[0]),
                        new NormalState(this));
                System.out.println("Created one symb " + new Symbol(statesAndTransitions[0]));
            }
        }
        // Make sure that the next state is created
        if (finalStates.contains(new Symbol(statesAndTransitions[2]))) {
            if (!createdStates.containsKey(new Symbol(statesAndTransitions[2]))) {
                createdStates.put(new Symbol(statesAndTransitions[2]),
                        new FinalState(this));
            }
        } else {
            if (!createdStates.containsKey(new Symbol(statesAndTransitions[2]))) {
                createdStates.put(new Symbol(statesAndTransitions[2]),
                        new NormalState(this));
            }
        }

        System.out.println(createdStates);
        // Define the transition
        createdStates.get(new Symbol(statesAndTransitions[0])).addTransition(
                new Symbol(statesAndTransitions[1]),
                createdStates.get(new Symbol(statesAndTransitions[2])));

    }
    this.currentState = createdStates.get(new Symbol("0"));
}

public String analyzeInput(String input) {
    String splitInput[] = input.split("\\s+");
    for(String s: splitInput)
        try {
            currentState.transition(new Symbol(s));
        } catch (TrapException e) {
            return("Reject");
        }
    if(currentState.type()==0)
        return "Accept";
    return "Reject";
}


public void setState(State currentState) {
    this.currentState = currentState;
}
 }




public class Symbol<E> {
private E symbol;

public Symbol(E symbol){
    this.symbol = symbol;
}

public E getSymbol() {
    return symbol;
}

public void setSymbol(E symbol) {
    this.symbol = symbol;
}

public String toString(){ return "" +symbol;}

}

Lightyear Buzz
  • 796
  • 7
  • 28
  • 1
    more information needed ... what exactly are you trying to do ? code snippets would be helpful as well – giorashc Apr 23 '12 at 08:44
  • Please show us your code, and the exact errors you are getting. [Have you implemented both `equals` and `hashCode` properly on your key objects?](http://stackoverflow.com/questions/27581/overriding-equals-and-hashcode-in-java) – Péter Török Apr 23 '12 at 08:45
  • Another important thing to consider is that HashMap keys should be immutable (at least as long as they are in the Map). If a key changes its hashCode after it has been placed in the HashMap, the lookup won't work very well. – Thilo Apr 23 '12 at 08:55
  • In case anyone needs a quick method I fixed it by going to the following under eclipse: source --> generate hashcode and equals – Lightyear Buzz Apr 23 '12 at 09:13

6 Answers6

3

Yes it does. However, if you don't define your own equals() for your own class, it uses Object.equals(), and that does use ==. This is why you should override equals() (and hashcode()) if you ever want to put your objects into a Collection.

Kilian Foth
  • 13,904
  • 5
  • 39
  • 57
  • if you ever want to use your object as keys in a HashMap. Other uses in Collections may be fine with the default implementation. – Thilo Apr 23 '12 at 08:49
  • 1
    It's overly broad to say that you should override 'equals' and 'hashCode' if you want to put objects in a collection. You don't need to override either to put them in a `List`; if you want to put them in a `TreeMap` or `TreeSet`, you need to override `equals`, but not `hashCode`, but you probably do need to implement `Comparable`. – Tom Anderson Apr 23 '12 at 08:50
  • @TomAnderson: You still may need `equals` in a `List` if you want to use things like `indexOf`. – Thilo Apr 23 '12 at 08:51
  • @TomAnderson: Actually, if you put them in a `TreeMap` you *should* override `equals()`, and not need to. And if you override `equals()`, according to the convention, you *should* also override `hashCode()`. according to the javadocs of [comparator](http://docs.oracle.com/javase/6/docs/api/java/util/Comparator.html#compare%28T,%20T%29): `It is generally the case, but not strictly required that (compare(x, y)==0) == (x.equals(y)).` – amit Apr 23 '12 at 08:52
  • @amit: If instance identity is fine for you, you don't need to override equals. – Thilo Apr 23 '12 at 08:52
  • @Thilo: The `TreeMap`, `TreeSet`, etc.. do not check for equality by using `equals()`, two elements `e1`,`e2` are equal if `compare(e1,e2) == 0` - so I don't understand your comment :\ – amit Apr 23 '12 at 09:52
  • @amit: I misread your comment. This was more about using it in things like ArrayList, to give another example against "if you ever want to put your objects into a Collection", to show that you don't need to do anything at all in some cases. For comparator-based collections, you need Comparable (and nothing else), just like you say. – Thilo Apr 23 '12 at 10:15
2

It uses hashcode() to locate potential matches and then uses equals to find exact match. If its user defined object make sure that both equals and hashcode are implemented to honor the contract (mentioned in Class documentation of Object).

Ashwinee K Jha
  • 9,187
  • 2
  • 25
  • 19
1

get first uses == to check that maybe it's the same object. If not it uses equals.

See the code here http://www.docjar.com/html/api/java/util/HashMap.java.html

/**
  298        * Returns the value to which the specified key is mapped,
  299        * or {@code null} if this map contains no mapping for the key.
  300        *
  301        * <p>More formally, if this map contains a mapping from a key
  302        * {@code k} to a value {@code v} such that {@code (key==null ? k==null :
  303        * key.equals(k))}, then this method returns {@code v}; otherwise
  304        * it returns {@code null}.  (There can be at most one such mapping.)
  305        *
  306        * <p>A return value of {@code null} does not <i>necessarily</i>
  307        * indicate that the map contains no mapping for the key; it's also
  308        * possible that the map explicitly maps the key to {@code null}.
  309        * The {@link #containsKey containsKey} operation may be used to
  310        * distinguish these two cases.
  311        *
  312        * @see #put(Object, Object)
  313        */
  314       public V get(Object key) {
  315           if (key == null)
  316               return getForNullKey();
  317           int hash = hash(key.hashCode());
  318           for (Entry<K,V> e = table[indexFor(hash, table.length)];
  319                e != null;
  320                e = e.next) {
  321               Object k;
  322               if (e.hash == hash && ((k = e.key) == key || key.equals(k)))
  323                   return e.value;
  324           }
  325           return null;
  326       }
Alexander Kulyakhtin
  • 47,782
  • 38
  • 107
  • 158
1

It uses the hashCode() for the comparison - http://www.docjar.com/html/api/java/util/HashMap.java.html

scibuff
  • 13,377
  • 2
  • 27
  • 30
0

Hashmap uses the hashcode() to find the appropirate bucket first and if it finds more than one entry in bucket then it used equals method.

If you are not defining equals method in your class as per your requirment than it will use the defination from parent class Object, which is simple "==" operation.

It is always preferable to override hashcode and equals method if you are using your class as key in hashmap.

Panther
  • 3,312
  • 9
  • 27
  • 50