3

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;
    }
}

5 Answers 5

1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks alot. and why this has been written overthere. K extends Comparable<K>. Is K is an interface there?
K could be an interface, but most likely it will not be an interface. Comparable is an interface. I added a bit about the Comparable<K> part. Enjoy. Note that I changed the example a little bit, as Number doesn't implement Comparable, but Date does, and it orders the dates linearly on a timeline.
0

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

Comments

0

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.

Comments

0

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.

1 Comment

I think this should be a comment rather than answer
0

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'

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.