QA's approach 2 Java - Comparable & Comparator Interfaces
Comparable Interface
Available in java.lang package, hence by default available to all the classes.
abtract method: public int compareTo(Object arg0)
Used for Natural sorting order - Ascending order
Can be overridden to be utilized for sorting based on our requirement for our class object.
Comparator Interface
Available in java.Utilpackage
abtract method: public int compare(Object arg0, Object arg1)
Used for Customized sorting order
Need to implement the logic for this abstract method according our requirement for achieving the required sorting order.
How to sort elements of arrays and Wrapper classes?
Lists and arrays of objects implement Comparable interface, hence can be sorted automatically by Collections.sort (and Arrays.sort).We can sort elements of arrays and Wrapper classes, since they already implements Comparable.
Example:
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
public class Demo {
public static void main(String[] args) {
/*
* Integer class implements Comparable
* Interface so we can use the sort method
*/
int[] arr = {11,55,22,0,89};
Arrays.sort(arr);
System.out.print("Sorted Int Array: ");
System.out.println(Arrays.toString(arr));
/*
* String class implements Comparable
* Interface so we can use the sort method
*/
System.out.print("Sorted String Array: ");
String[] names = {"Steve", "Joe", "Kallis"};
Arrays.sort(names);
System.out.println(Arrays.toString(names));
/*
* String class implements Comparable
* Interface so we can use the sort method
*/
System.out.print("Sorted List: ");
List fruits = new ArrayList();
fruits.add("Orange");
fruits.add("Banana");
fruits.add("Apple");
fruits.add("Guava");
fruits.add("Grapes");
Collections.sort(fruits);
for(String s: fruits) System.out.print(s+", ");
}
}
public static void main(String[] args) {
/*
* Integer class implements Comparable
* Interface so we can use the sort method
*/
int[] arr = {11,55,22,0,89};
Arrays.sort(arr);
System.out.print("Sorted Int Array: ");
System.out.println(Arrays.toString(arr));
/*
* String class implements Comparable
* Interface so we can use the sort method
*/
System.out.print("Sorted String Array: ");
String[] names = {"Steve", "Joe", "Kallis"};
Arrays.sort(names);
System.out.println(Arrays.toString(names));
/*
* String class implements Comparable
* Interface so we can use the sort method
*/
System.out.print("Sorted List: ");
List fruits = new ArrayList();
fruits.add("Orange");
fruits.add("Banana");
fruits.add("Apple");
fruits.add("Guava");
fruits.add("Grapes");
Collections.sort(fruits);
for(String s: fruits) System.out.print(s+", ");
}
}
How to sort elements of custom objects using Comparable Interface?
If we want to sort the objects of custom class then you need to implement the Comparable interface in our custom class.
public abstract int compareTo(T obj)
Since this method is abstract, we must implement this method in our class if we implement the Comparable interface.
Comments
Post a Comment