Quand java appelle la méthode get de hashmap java effectue une comparaison equals()? J'ai lu que c'est le cas mais avec les erreurs que je reçois, il semble que ce soit une comparaison ===.Comment fonctionne get() sur hashmap
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;}
}
plus d'informations nécessaires ... qu'est-ce que vous essayez de faire exactement? les extraits de code seraient également utiles – giorashc
Veuillez nous indiquer votre code et les erreurs exactes que vous obtenez. [Avez-vous implémenté à la fois 'equals' et' hashCode' correctement sur vos objets clés?] (Http://stackoverflow.com/questions/27581/overriding-equals-and-hashcode-in-java) –
Une autre chose importante à considérer est que les clés HashMap doivent être immuables (au moins aussi longtemps qu'elles sont dans la carte). Si une clé change son hashCode après qu'elle a été placée dans HashMap, la recherche ne fonctionnera pas très bien. – Thilo