previous | start | next

Writing a Class for use with Hash tables (Java)

Consider a new class type that you will use as the key for insertions into a HashSet or HashMap. For example:

        public class TokenPos
        {
           private int level;
           private String token;
           ...
           public boolean equals(Object other) {...}
           public int hashCode() {...}
           ...
        }
     

How/why should we implement equals and hashCode for this class?



previous | start | next