Java 我应该如何实现这个HashMap';s equals和hashCode方法来表示自动机状态?
我想将状态对象(以字符作为键,以状态作为值的哈希映射)放入名为AllState的ArrayList中。我应该在这里重写equals和hashCode方法吗?为什么?如何 这段代码适用于我迄今为止构建的自动机和状态类:Java 我应该如何实现这个HashMap';s equals和hashCode方法来表示自动机状态?,java,hashmap,equals,hashcode,automaton,Java,Hashmap,Equals,Hashcode,Automaton,我想将状态对象(以字符作为键,以状态作为值的哈希映射)放入名为AllState的ArrayList中。我应该在这里重写equals和hashCode方法吗?为什么?如何 这段代码适用于我迄今为止构建的自动机和状态类: class State extends HashMap<Character, State>{ boolean isFinal; boolean isInitial; int stateId; State () { isInitial=false; i
class State extends HashMap<Character, State>{
boolean isFinal;
boolean isInitial;
int stateId;
State () {
isInitial=false;
isFinal = false;
}
public boolean equals (Object o){
boolean isEqual = false;
State compare = (State)o;
if ((compare.stateId)==this.stateId)
{
return true;
}
return isEqual;
}
public int hashCode() {
int theHashCode = stateId%7;
return theHashCode;
}
}
class Automaton{
List <State> allStates;
//private List<State> finalStates;
int theInitialStateIntIndex;
State actualState;
char [] alphabet;
Automaton() {
allStates = new ArrayList<State>();
}
public void setAllStates (int numberOfStates) {
for (int i =0; i <numberOfStates; i++) {
State newState = new State();
newState.stateId = i;
allStates.add(newState);
}
}
public void setAlphabet (String alphabetLine){
alphabet = alphabetLine.toCharArray();
}
public void markFinalStates (String [] finalStates){
for (int index =0; index<finalStates.length; index++) {
int aFinalStateId = Integer.parseInt(finalStates[index]);
State aFinalState = allStates.get(aFinalStateId);
aFinalState.isFinal = true;
allStates.add(aFinalStateId, aFinalState);
/*DEBUG*/
aFinalState = allStates.get(aFinalStateId);
if ((aFinalState.isFinal)==true)
System.out.println("THE STATE " + aFinalStateId + " IS MARKED AS FINAL");
}
}
public void markInitialState (int initialStateId) {
State theInitialState = allStates.get(initialStateId);
theInitialState.isInitial=true;
allStates.add(initialStateId, theInitialState);
theInitialStateIntIndex = initialStateId;
/*DEBUG*/
System.out.println("THE INITIAL STATE ID IS " + initialStateId);
theInitialState = allStates.get(initialStateId);
if ((theInitialState.isInitial)==true)
System.out.println("THE STATE " + initialStateId + " IS MARKED AS INITIAL");
}
public void setTransitions(int stateId, String transitionsLine){
State theOneToChange = allStates.get(stateId);
String [] statesToReachStringSplitted = transitionsLine.split(" ");
for (int symbolIndex=0; symbolIndex<statesToReachStringSplitted.length;symbolIndex++){
int reachedState= Integer.parseInt(statesToReachStringSplitted[symbolIndex]);
theOneToChange.put(alphabet[symbolIndex],allStates.get(reachedState));
System.out.println("THE STATE " + stateId + " REACHES THE STATE " + reachedState + " WITH THE SYMBOL " + alphabet[symbolIndex]);
}
allStates.add(stateId, theOneToChange);
}
public int findInitialState(){
int index =0;
cycle: for (; index<allStates.size(); index++){
State s = allStates.get(index);
if (s.isInitial==true) {
break cycle;
}
} return index;
}
public void processString (String string)
{
StringBuilder stepString= new StringBuilder (string);
int actualStateIntIndex;
System.out.println("THE FOUND INITIAL ONE IS "+ theInitialStateIntIndex);
State firstState = allStates.get(theInitialStateIntIndex);
actualState = firstState;
while (stepString.length()>0){
Character characterToProcess = stepString.charAt(0);
stepString.deleteCharAt(0);
State nextState;
nextState = ((State)actualState.get(characterToProcess)); // pasa al siguiente State
actualState = nextState;
actualStateIntIndex=allStates.indexOf(actualState);
System.out.println("the actual state for " + stepString + " is " + actualStateIntIndex);
if ((actualState.isFinal==true) && (stepString.length()==0))
{
System.out.println("THE STRING " + string + " IS ACCEPTED AT STATE " + actualStateIntIndex );
}
else if (stepString.length()==0 && (actualState.isFinal==false)){
System.out.println("THE STRING " + string + " IS REJECTED AT STATE " + actualStateIntIndex);
}
}
}
}
类状态扩展HashMap{
布尔是最终的;
布尔初始值;
int stateId;
国家(){
isInitial=false;
isFinal=false;
}
公共布尔等于(对象o){
布尔相等=假;
状态比较=(状态)o;
if((compare.stateId)=此.stateId)
{
返回true;
}
回报均等;
}
公共int hashCode(){
int theHashCode=stateId%7;
返回hashcode;
}
}
类自动机{
列出所有国家;
//私人名单最终状态;
初始状态指数;
状态实际状态;
字符[]字母表;
自动机(){
allStates=newarraylist();
}
public void setAllStates(int numberOfStates){
对于(int i=0;i
如果自动机是DFA,则可以使用访问字符串标识字段。
访问字符串是可用于从开始状态到达状态的任何字符串。在构建DFA时必须构建该字符串,但是,这不会增加更多的时间复杂性。(是的,然后需要对该字符串进行哈希编码/相等)
或者,实际上,通过增加序列号/字符串来标识状态应该适用于所有自动机
选择第二个,它比1更简单,效果更好,除非你想处理重复的状态
是的,用户定义的类型需要hashcode和equals才能使用hash。对不起,我不明白:您想在哪里重写它们?State对象的equals()和hashcode()有什么问题?