Let’s say I have a CategoryKey class:
public class CategoryKey {
public int layer;
public int parent;
public int child;
}
I try to put many instances of it within a Set, but as we know HashSet is not suitable to store instances of custom classes. So how can I extend the java class HashSet to meet my requirement? Or how can I create a new class implementing interface Set to solve the same problem?
Yes it is, so long as you write your class appropriate. In particular:
equals()andhashCode()For example: