A List contains objects that you can order according to a natural order using the static method Collections.sort(List list) if the objects implement the Comparable interface.
The only method required by the Comparable interface is compareTo which has as argument an object of the same type you are sorting and the return value is an integer.
So the call to the method is:
int result = x.compareTo(y)
where x and y are objects of the same type of those in the list and result is an integer that takes the value:
<0 if x<y
==0 if x==y
>0 if x>y
Look the follow example:
- Main.java
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556import java.util.ArrayList;import java.util.Collections;import java.util.List;public class Main {public static void main(String[] args) {// FIRST SECTIONList<String> myStringList = new ArrayList<String>();myStringList.add(new String("Germany"));myStringList.add(new String("French"));myStringList.add(new String("United Kingdom"));myStringList.add(new String("Italy"));myStringList.add(new String("Spain"));System.out.println("UNSORTED LIST:");for (String item : myStringList) {System.out.println(item);}// SECOND SECTIONSystem.out.println("\nSORTED LIST:");Collections.sort(myStringList);for (String item : myStringList) {System.out.println(item);}// THIRD SECTIONList<Country> myCountryList = new ArrayList<Country>();Country germany = new Country("Germany", 357021);Country french = new Country("French", 547030);Country unitedKingdom = new Country("United Kingdom", 244820);Country italy = new Country("Italy", 301230);Country spain = new Country("Spain", 504851);myCountryList.add(germany);myCountryList.add(french);myCountryList.add(unitedKingdom);myCountryList.add(italy);myCountryList.add(spain);System.out.println("\nSORTED LIST BY AREA:");Collections.sort(myCountryList);for (Country item : myCountryList) {System.out.println(item.getName());}}} - Country.java
123456789101112131415161718192021222324252627282930313233public class Country implements Comparable<Country> {private String name;private int area;public Country(String name, int area) {this.name = name;this.area = area;}public String getName() {return name;}public void setName(String name) {this.name = name;}public int getArea() {return area;}public void setArea(int area) {this.area = area;}@Overridepublic int compareTo(Country o) {int result;result = area - o.getArea();return result;}}
The output is as follows:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
UNSORTED LIST: Germany French United Kingdom Italy Spain SORTED LIST: French Germany Italy Spain United Kingdom SORTED LIST BY AREA: United Kingdom Italy Germany Spain French |
In the first section the list is printed in the order in which the elements have been added to the list.
In the second section you can sort with Collections.sort because the String class implements the Comparable interface and the order is alphabetic.
In the third section, the list contains objects (Country) that implement Comparable and then the sort is ascending by area because of I implemented the compareTo method.
Similarly, you can also sort the arrays using the Arrays.sort(Object[] a) method.
One reply on “The Comparable interface”
[…] Site ← The Comparable interface […]