user2454830
user2454830

Reputation: 193

Java - Comparable

I am new to java. Can anyone give the meaning of the following class declaration

public class ListNode<K extends Comparable<K>, V> {
    K key;
    V value;
    ListNode<K, V> next;
    ListNode(K key, V value,ListNode<K,V> next) {
        this.key = key;
        this.value = value;
        this.next = next;
    }
}

Upvotes: 3

Views: 1346

Answers (5)

Edwin Buck
Edwin Buck

Reputation: 70909

There is a class, which allows the construction of new ListNodes. Each time you construct a ListNode you are required to indicate the type of ListNode you want, where part of the type is a different class that implements the Comparable interface and the other class can be anything.

ListNode<Date, String> node = new ListNode<>();

is an example.

With such an example, you would then have a ListNode that looked more like:

public class ListNode {
    Date key;
    String value;
    ListNode<Date, String> next;
    ListNode(Date key, String value, ListNode<Date,String> next) {
        this.key = key;
        this.value = value;
        this.next = next;
    }
}

The stuff between the < and > is the generic type, and is a feature of Java better known as "Java Generics". You should read up on it. It is slightly beyond the scope of a complete explanation within one post, but hopefully the above example will give you an idea. It allows one to write a class that generically takes a pseudo-type-argument.

The comparable interface is a special interface which provides a linear ordering of some type. For example, you could do alphabetical ordering of Strings, in which case you would have something that looked like

public StringComparator implements Comparable<String> ....

it works by the Comparable keyword forcing an implementation of a method to be defined.

public int compareTo(String other) {
  ...
}

Where if the returned integer is negative the other comes before this item, if the returned value is positive, the other comes after this item, and if the returned value is zero, then the two items are (for ordering purposes) equivalent.

Upvotes: 1

wfwei
wfwei

Reputation: 95

This 'Class' definitoin is special only because it use 'generic' type parameter? if you do not know what 'generic' means, I think you should read a java book before read/write java code like this ~

class ListNode<K, V> means the ListNode class uses two 'unknown' or 'general' variable type (i.e. K V), you can indicate the specific type when you use the class

K extends Comparable<K> meands that the 'K' type is restricted to be 'Comparable'

Upvotes: 0

prasanth
prasanth

Reputation: 3602

You must have heard about Generics in java. If not, you could have a look at this http://docs.oracle.com/javase/tutorial/extra/generics/index.html. Answer lies right there.

Upvotes: 0

Sachin Verma
Sachin Verma

Reputation: 3802

Every class that implements Comparable interface means the objects of that class can be compared to each other and that comparison can be used in many utilities like making a tree (TreeSet).
In your case:

ListNode<K extends Comparable<K>, V>

K could be anything that implements comparable like Number its Subclasses or your custom class.

You: what do comparable do to the class that its objects can be compared.

Ans: It got a compareTo(T t) method that you can write how you want to tell one object is greater or equal to or smaller than comparing object by returning an int.

Upvotes: 0

Prasad Kharkar
Prasad Kharkar

Reputation: 13556

THe statement public class ListNode<K extends Comparable<K>, V> means that

  • ListNode is a parameterized class with key that implements Comparable for a generic type.
  • V is the value for key

Upvotes: 0

Related Questions