.NET port with Java's Map, Set, HashMap
        Posted  
        
            by 
                Nikos Baxevanis
            
        on Stack Overflow
        
        See other posts from Stack Overflow
        
            or by Nikos Baxevanis
        
        
        
        Published on 2011-11-12T09:33:19Z
        Indexed on 
            2011/11/12
            9:51 UTC
        
        
        Read the original article
        Hit count: 256
        
I am porting Java code in .NET and I am stuck in the following lines that (behave unexpectedly in .NET).
Java:
Map<Set<State>, Set<State>> sets = new HashMap<Set<State>, Set<State>>();
Set<State> p = new HashSet<State>();
if (!sets.containsKey(p)) { ... }
The equivalent .NET code could possibly be:
IDictionary<HashSet<State>, HashSet<State>> sets = new Dictionary<HashSet<State>, HashSet<State>>();
HashSet<State> p = new HashSet<State>();
 if (!sets.containsKey(p)) { /* (Add to a list). Always get here in .NET (??) */ }
However the code comparison fails, the program think that "sets" never contain Key "p" and eventually results in OutOfMemoryException.
Perhaps I am missing something, object equality and identity might be different between Java and .NET.
I tried implementing IComparable and IEquatable in class State but the results were the same.
Edit:
What the code does is: If the sets does not contain key "p" (which is a HashSet) it is going to add "p" at the end of a LinkedList>.
The State class (Java) is a simple class defined as:
public class State implements Comparable<State> {
boolean accept;
Set<Transition> transitions;
int number;
int id;
static int next_id;
public State() {
    resetTransitions();
    id = next_id++;
}
// ...
public int compareTo(State s) {
    return s.id - id;
}
public boolean equals(Object obj) {
    return super.equals(obj);
}
public int hashCode() {
    return super.hashCode();
}
© Stack Overflow or respective owner