Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Class definition class name <>

Tags:

java

generics

What's the sense of this class definition, what's kind of class is that?

class Node<K extends Comparable<? super K>,V>
like image 684
Ariel Grabijas Avatar asked Dec 07 '11 20:12

Ariel Grabijas


3 Answers

This is a generic class definition. It translates to:

  • class Node takes two types as parameters: K and V.
  • the type K must extend the class Comparable
  • the class Comparable itself, in this case, takes some type as parameter, let's call it T.
  • type T must be a superclass of K.

Edit: Ok, since an example was requested, a simple instantiation of this class could be:

Node<Integer, String> node = new Node<Integer, String>();

Since the Integer class implements Comparable<Integer> it fits the above description nicely (note that super also allows type T to the the same type as K).

V has no constraints, so it can be any type.

like image 105
Tudor Avatar answered Oct 01 '22 18:10

Tudor


It's a generic class of types K and V, where K is a type that extends Comparable of any class that is a superclass of K.

like image 22
Luchian Grigore Avatar answered Oct 03 '22 18:10

Luchian Grigore


Looks like it's from an implementation of a red-black tree designed for explanatory purposes:

Red-black tree implemented in Java

Beyond that, it's a class called Node that takes parameters K and V, where K extends Comparable, which takes a parameter that is itself a superclass of K.

like image 1
Shaun Avatar answered Oct 03 '22 18:10

Shaun