Reputation: 377
i m working with collections and i cant figure this out... I want to override compareTO() method based on the "data" variable in the Node class.. so that i can invoke collection.sort() to sort the arraylist..
public class Node<E> implements Comparable<E>{
public E data;
public Node<E> next;
public Node(){
data=null;
next=null;
}
public Node(E data1){
data=data1;
}
public E getData(){
return data;
}
public Node<E> getNext(){
return next;
}
@Override
public int compareTo(E o) {
// TODO Auto-generated method stub
return 0;
}
}
And
public class Test {
public static void main(String args[]){
ArrayList<Node> arr= new ArrayList<Node>();
Node n1=new Node(1);
Node n2=new Node(3);
Node n3=new Node(4);
Node n4=new Node(3);
Node n5=new Node(6);
Node n6=new Node(2);
arr.add(n1);
arr.add(n2);
arr.add(n3);
arr.add(n4);
arr.add(n5);
arr.add(n6);
Collections.sort(arr);
}
}
Upvotes: 3
Views: 10485
Reputation: 1503489
Your declaration looks odd to me. It would be odd to compare a Node<Integer>
with an Integer
for example - it would make more sense to compare a Node<Integer>
with another Node<Integer>
by comparing the integers within them.
You then need to constrain E
such that you can compare the two data values.
So I suspect want:
public class Node<E extends Comparable<E>> implements Comparable<Node<E>> {
...
public int compareTo(Node<E> node) {
return data.compareTo(node.data);
}
}
You could make this slightly more flexible at the cost of complexity, like this:
public class Node<E extends Comparable<? super E>> implements Comparable<Node<E>>
(The body of the code would remain the same...)
Upvotes: 3
Reputation: 62469
What you're doing is basically delegating the comparison of the Node
to the comparison of the runtime type E
, so E
itself should also implement Comparable
. So I think you meant:
class Node<E extends Comparable<E>> implements Comparable<Node<E>> {
private E data;
@Override
public int compareTo(Node<E> arg0) {
return arg0.data.compareTo(data);
}
}
Also note that you should be comparing Node
objects to Node
objects, not Node
objects to E
objects.
Side-note: Your class members should be private. You even have getters, so why are they public?
Upvotes: 0
Reputation: 340983
Basically the E
element of your collection must also be comparable, which can be enforced in the following way:
public class Node<E extends Comparable<E> implements Comparable<Node<E>> {
Now in compareTo()
you just compare your elements:
@Override
public int compareTo(Node o) {
return data.compareTo(o.data);
}
If your Node
class accepts null
data element, compareTo()
must be implemented more carefully.
Upvotes: 0