• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Sorting an ArrayList

 
Ranch Hand
Posts: 324
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all

Can anyone explain below signature of Collections.sort() method


Thanks
 
Ranch Hand
Posts: 558
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Amir,
Given signature) public static <T extends Comparable<? super T>> void sort(List<T> list)
First of all this generic method. A generic method should have the signature
like ex: public <T> void display(List<T> list)
<T> before return type is a must for generic method and can be used to filter some unwanted inputs types.

step 1) Suppose we call like this
List<String> list = New ArrayList();
list.add("Kathy");
list.add("Bert");
list.add("SCJP");
Collections.sort(list);
step 2)
newly generated signature >>
public static <String extends Comparable<? super String>> void sort(List<String> list)

step 3)
filter condition : <String extends Comparable<? super String> means

1) String should extend Comparable interface(here extend can also be used for interface.) So String will pass this test.and one more

2) Comparable<? super String> means sort method can add Strings to our list<String>.

Hope its very clear. This one has clear explanation in K&B book.
and see also what <? extends T> is used for. K&B book has given Best explanation on why its introduced.

All the best
 
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Amir,



We will first define a generic class say MyList;

class Person implements Comparable<Person> {
public int compareTo(Person p1) {
return name.compareTo(p1.name);
}
private String name;
}

public class MyList<T extends Comparable> {

void add(T a) {
}
public static void main(String[] args) {

}
}

We have a generic MyList that can be parameterized with any class that implements Comparable interface. Yeah we have Person class that suits to it. Ok inside the main() method we do like this:

MyList<Person> list1 = new MyList<Person>();//good

Parameterized type Person is a good substitute for what exactly our generic class definition of MyList permits. Although all wrapper classes can substitute the Person as well because they all implement the Comparable interface, but we pay attention to non-standard classes that are our own as Person.

Ok lets extend our activity by making one more class say Driver that extends Person

class Driver extends Person {
}

Obviously Driver class implements the Comparable interface because its daddy implements so. Now the big gotcha: See this

MyList<Driver> mylist1 = new MyList<Driver>();
//Unfortunately you are not allowed to do so. MyList expects here that Driver do also implement the Comparable interface, inheritance of interface implementation from the Parent class wont do here. Don�t you feel it�s overkill. Parent class implements the interface and Child class is forced to implement the same; what the hell obligation is this?


There is something that is at rescue. If you modify the MyList generic class as :

public class MyList<T extends Comparable<? super T>> {


}

Now it is ok! What does that mean? It means still parameterized type must be be class that implements Comparable interface as T extends Comparable says but it that unknown class�s (represented by wildcard �?�) superclass implements the Comparable , that will also do, that is what we wanted to have in our Person-Driver relationship.

Now this is allowed:

MyList<Driver> mylist2 = new MyList<Driver>();//Now it is ok!


Little more talk is left:

public class MyList<T extends Comparable> {
}

This generic class definition is less restrictive than

public class MyList<T extends Comparable<T>> {
}

The first one says, that all the elements all allowed if at least the parent class implements the Comparable interface.

The second one restricts you to by saying this; all the elements must be comparable or each element added to the Mylist must be object of the class that implements the Comparable interface.

You make the second definition flexible by doing that:
public class MyList<T extends Comparable<? super T>> {
}

It means, if the super class of the added element implements the Comparable interface, it will do.

I see following two class definitions conveying the same meaning! Don�t you?

public class MyList<T extends Comparable>{}

and

public class MyList<T extends Comparable<? super T>>{}



I hope this explanation helps you!
reply
    Bookmark Topic Watch Topic
  • New Topic