package com.company;
import java.util.TreeSet;
public class Main {
public static class Node implements Comparable<Node>
{
public int value;
public Node(int value) {
this.value = value;
}
@Override
public int compareTo(Node node) {
// Memory location of this - memory location of node.
return 0;
}
}
public static void main(String[] args) {
TreeSet<Node> set = new TreeSet<>();
Node n = new Node(5);
set.add(n);
for (var node : set)
System.out.println(node.value);
}
}
Here I have a Node
class. I want to be able to insert the nodes in a TreeSet
and sort them by their location in memory. How can I return the difference of the memory locations in the function compareTo
?