java.lang.Comparable
--------------------------
int compareTo(Object o) : This method compares 'this' object with 'o' object--------------------------
if the int value is
positive - 'this' object is greater than 'o' object
zero - 'this' object is equals with 'o' object
negative - 'this' object is less than 'o' object
java.util.Comparator
-----------------------------
int compare(Object o1,Object o2) : This method compares o1 and o2 objects.
if the int values is
positive - o1 is greater than o2
zero - o1 equals o2
negative - o1 is less than o2
A simple example
showing how to use Comparable and Comparator interfaces :
First create a pojo called NetworkProvider.java that implements Comparable interface.
package com.sample.sort; public class NetworkProvider implements Comparable<Object> { private String netName; private String headQuarters; private Integer positionInIndia; public NetworkProvider(String name, String hq, int position) { this.netName = name; this.headQuarters = hq; this.positionInIndia = position; } public String getNetName() { return netName; } public void setNetName(String netName) { this.netName = netName; } public String getHeadQuarters() { return headQuarters; } public void setHeadQuarters(String headQuarters) { this.headQuarters = headQuarters; } public Integer getPositionInIndia() { return positionInIndia; } public void setPositionInIndia(Integer positionInIndia) { this.positionInIndia = positionInIndia; } @Override public int compareTo(Object o) { NetworkProvider n = (NetworkProvider) o; return this.getPositionInIndia().compareTo(n.getPositionInIndia()); } }
Now create a class called GetNetworkDetails.java that will add the Network details to ArrayList.
package com.sample.sort; import java.util.ArrayList; import java.util.List; public class GetNetworkDetails { public static List<NetworkProvider> getNetworkDetails() { List<NetworkProvider> list = new ArrayList<NetworkProvider>(); list.add(new NetworkProvider("Vodaone", "London", 3)); list.add(new NetworkProvider("Airtel", "India", 2)); list.add(new NetworkProvider("BSNL", "India", 1)); return list; } }
If we want the ordering by 'name' preference(you can do with any field) then implement the comparator interface, without touching the original code.
SortByName.java package com.sample.sort; import java.util.Comparator; public class SortByName implements Comparator<NetworkProvider> { @Override public int compare(NetworkProvider n1, NetworkProvider n2) { return n1.getNetName().compareTo(n2.getNetName()); } }
Now create a class NetworkMain.java that will show the details.
package com.sample.sort; import java.util.Collections; import java.util.List; public class NetworkMain { public static void display(List<NetworkProvider> list) { for (NetworkProvider n : list) { System.out.print("Position :" n.getPositionInIndia() "\t"); System.out.print("Name : " n.getNetName() "\t"); System.out.print("HeadQuarters : " n.getHeadQuarters() "\n"); } } public static void main(String[] args) { List<NetworkProvider> list = GetNetworkDetails.getNetworkDetails(); System.out .println("Sort by Natural order" "\n" "*********************"); Collections.sort(list); display(list); System.out.println("\n" "Sort by Name" "\n" "*********************"); Collections.sort(list, new SortByName()); display(list); } }
output:
Sort by Natural order
*********************
Position :1 Name : BSNL HeadQuarters : India
Position :2 Name : Airtel HeadQuarters : India
Position :3 Name : Vodaone HeadQuarters : London
Sort by Name
*********************
Position :2 Name : Airtel HeadQuarters : India
Position :1 Name : BSNL HeadQuarters : India
Position :3 Name : Vodaone HeadQuarters : London
No comments:
Post a Comment