How to understand <t extends comparable< in Java? Super t>>

Source: Internet
Author: User
Tags comparable

1 <T extends Comparable<T>> and <T extends Comparable<? super T>> What's the difference
<T extends Comparable<T>>
The
type T must implement the Comparable interface, and the type of the interface is T. Only in this way can the instances of T compare size to each other. For example, if the specific class used in the actual call is dog, then dog implements Comparable<Dog> must
<T extends Comparable<? super T>>
The
type T must implement the Comparable interface, and the type of the interface is the any parent class of T or T. After this declaration, the instances of T between instances of T and instances of its parent class can be compared to each other size. For example, if the specific class used when actually called is dog (assuming that dog has a parent class Animal), the dog can inherit from Animal Comparable<Animal> , or itself implements Comparable<Dog> .
2 My <T extends Comparable<? super T>> understanding of the type parameters

The definition above does not have any other feeling except that it can not touch the mind. The following code is used to explain why this is declared.

2.1 Code Runtime Environment

The JDK version I used was: 1.8.0_60 compile and run in Eclipse. Because the annotations are in Chinese, the codes are UTF-8. If you want to compile and run at the command line, use the option at compile time -encoding UTF-8 :

Javac-encoding UTF-8 Typeparametertest.java

In addition, the warnings and error messages in Eclipse are not the same as in the command line (personally feel that the information in eclipse is better understood). The following example takes the information in Eclipse as a standard.

2.2 Sample Code
1:package GENERICS3;2:3:import java.util.ArrayList;4:import java.util.Collections;5:import java.util.List;6:7:public class Typeparametertest8: {9://First statement: simple, low flexibility10:public static <t extends comparable<t>> void MySort1 (list<t> List)11: {12:collections.sort (list);13:}14:15://Second statement: complex, high flexibility16:public static <t extends Comparable<? Super t>> void MySort2 (list<t> List)17: {18:collections.sort (list);19:}20:21:public static void Main (string[] args)22: {23://In this method you create a Animal list and a Dog list, and then call two sorting methods respectively.24:}25:}26:27:class Animal implements Comparable<animal> 28: {29:protected int age; : 31:public Animal (int age) 32: 33: {34:this.age = age;  35:} 36:  37://Use Age compared to another instance size 38: @Override 39:public int compareTo (Animal other) 40: {41:return This.age-other.age;  42:}  43:} 44: 45:class Dog extends Animal 46: { 47:public Dog (int age) 48: {49:super (age); 50:}51:}          

The above code consists of three classes:

    1. AnimalImplements an Comparable<Animal> interface to compare the size of instances by age
    2. DogInherit from Animal .
    3. TypeParameterTestThe two sorting methods and methods for testing are provided in the class main() :
      • mySort1()Using <T extends Comparable<T>> type parameters
      • mySort2()Using <T extends Comparable<? super T>> type parameters
      • main()Test method. In this method you want to create one Animal List and one Dog List , and then call two sorting methods separately
2.3 Test MySort1 () method
1://Create a Animal List 2:list<animal> animals = new arraylist<animal> (); 3:animals.add (New Animal (25 )); 4:animals.add (New Dog (35)); 6://Create a Dog List 7:list<dog> dogs = new arraylist<dog> (); 8:dogs.add (New Dog (5)); 9:dog S.add (New Dog (18)); 11://Test MySort1 () method 12:mysort1 (animals);  13:mysort1 (dogs);         

Line 13 out of compile error. Eclipse says:

The method MySort1 (list<t>) in the type typeparametertest are not applicable for the arguments (list<dog>)

Why did it make a mistake? The type parameter of the MySort1 () method is <t extends comparable<t>>, which requires the type parameter to be of type T Comparable .

If the incoming is List<Animal> , no problem, because Animal implements Comparable<Animal> .

However, if the parameter passed in is List<Dog> problematic because the Dog does not implements Comparable<Dog> , it just inherits one from the Animal Comparable<Animal> .

I don't know if you're aware of that. The animals list actually contains a Dog instance. If you run into a similar situation (the subclass list cannot be passed into a method), consider putting the class instance into a parent list to avoid compilation errors.

2.4 Test MySort2 () method
 1:public static void main (string[] args)  2: { Span class= "Linenr" > 3://Create a Animal list 4:list<animal> animals = new arraylist< Animal> ();  5:animals.add (new Animal (25));  6:animals.add (new Dog (35));  7:  8://Create a Dog list 9:list<dog> dogs = new Arra Ylist<dog> (); 10:dogs.add (New Dog (5)); 11:dogs.add (new Dog (18)); 12:  13://Test MySort2 () method 14:mysort2 (animals); 15:mysort2 (dogs); 16:}              

Two method calls are not a problem. The second method can not only accept Animal implements Comparable<Animal> such parameters, but also receive: Dog implements Comparable<Animal> such parameters.

is 2.5 Dog OK implements Comparable<Dog> ?

Wouldn't it be possible to solve the previous compilation error if the Dog implements comparable<dog>?

1:class Dog extends Animal implements comparable<dog>2:  {3: Public      Dog (int. Age)4:      { 5:          super (age);  6:}7:}     

Unfortunately, there was a mistake. Eclipse says:

The interface comparable cannot is implemented more than once with different arguments:comparable<animal> and Compa Rable<dog>

That is, Dog has inherited one from the parent class Animal Comparable , and it can no longer implement one Comparable .

What if subclasses don't like the implementation of the parent class? Override the method of the parent class public int compareTo(Animal other) .

<T extends Comparable<? super T>> Benefits of 2.6 type parameter declarations

For Animal/dog the two classes that have a parent-child relationship: <T extends Comparable<? super T>> You can accept list<animal>, or you can receive list<dog>. And <T extends Comparable<T>> can only receive list<animal>

So, <t extends comparable< A type parameter such as Super t>> has fewer restrictions on the parameters passed in and increases the flexibility of the API. In general, the least restrictive type parameters are used in order to ensure type safety.

How to understand <t extends comparable< in Java? Super t>>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.