get()如何在hashmap上工作 [英] How does get() work on hashmap

查看:70
本文介绍了get()如何在hashmap上工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当Java从hashmap调用get方法时,java是否执行equals()比较?

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.

公共类UniversalFiniteStateAutomaton {

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;}

}

推荐答案

是的.但是,如果您没有为自己的类定义自己的equals(),则它使用Object.equals(),而确实使用==.这就是为什么如果您想将对象放入集合中,则应该覆盖equals()(和hashcode())的原因.

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.

这篇关于get()如何在hashmap上工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆