Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I use a custom class in a TreeSet?

Tags:

java

treeset

If I was using a Set similar to this:

Set<node> s=new TreeSet<node>();

class node {

  private int x;
  private int y;

}

Would this be acceptable, and since it's a TreeSet, would it also sort it?

like image 812
SNpn Avatar asked Sep 14 '11 12:09

SNpn


1 Answers

It's not going to be able to sort it without you implementing Comparable<Node>, and it won't really be an appropriate for set operations until you override equals() and hashCode(). (You don't have to override equals and hashCode for TreeSet to work, but it would make sense to do so.)

Something like this:

final class Node implements Comparable<Node> {

  private final int x;
  private final int y;

  Node(int x, int y) {
    this.x = x;
    this.y = y;
  }

  @Override public boolean equals(Object other) {
    if (!(other instanceof Node)) {
      return false;
    }
    Node otherNode = (Node) other;
    return x == otherNode.x && y == otherNode.y;
  }

  @Override public int hashCode() {
    return x * 31 + y * 17; // For example...
  }

  @Override public int compareTo(Node other) {
    // As of Java 7, this can be replaced with
    // return x != other.x ? Integer.compare(x, other.x) 
    //     : Integer.compare(y, other.y);

    if (x < other.x || (x == other.x && y < other.y)) {
      return -1;
    }
    return x == other.x && y == other.y ? 0 : 1;
  }
}

(Note that by convention the class name would be Node, not node.)

like image 101
Jon Skeet Avatar answered Oct 10 '22 06:10

Jon Skeet