Search code examples
javacomputer-sciencesetfinite-automata

Can I implement State Transitions for a DFA in Java using java.util.Set


I'm implementing a DFA as close as I can to the formal definition as a learning exercise (and blogging material)

I planned on using a java.util.Set where a set is involved in the definition.

The definition involves a set of tuples to define the legal state transitions: (state,symbol) -> nextState.

I have a Transition class with members state, symbol and nextState. I have implemented equals() and hashCode() to indicate that two Transitions are equal if they match on state and symbol. I then have a java.util.Set of Transition instances.

In my processing algorithm, I have the current state when I read the next symbol. I anticipated building a Transition object using these two to pull out the matching Transition from the Set, which would then tell me the next state, and I can iterate.

But - I don't see any way of extracting a member of a java.util.Set for further use. I can remove(Object o), but that just return boolean.

What am I doing wrong?


Solution

  • Set is probably not what you want to use for this. My recommendation would be to use a List< Transition>, or possibly a Map< State,List< Transition>>. I'm not sure which would be better without actually building it and doing some benchmarking.