Comparable and Comparator example


Comparable

 

/* Source Code- Example */

/* ------------ Sorting thru Comparable Interface (Natural Sorting) ---------
 * Implements mandatory Comparable interface
 */
 class Person implements Comparable<Object>{

String firstname;
 String lastname;
 public Person(String firstname, String lastname) {
 super();
 this.firstname = firstname;
 this.lastname = lastname;
 }

public String getFirstname() {
 return firstname;
 }
 public String getLastname() {
 return lastname;
 }
 /*
 * return -1 : If this object is lesser than the passed object</pre>
<div>        return  0 : If this object is same the passed object
return  1 : If this object is greater than the person */</div>

public int compareTo(Object obj){
 Person person=(Person) obj;
 return this.firstname.compareTo(person.getFirstname());
 }
 }

public class <b>PersonComparable</b> {

public static void main(String[] args) {

List<Person> person=new ArrayList<Person>();

 person.add(new Person("Rajiv","Srivastava"));
 person.add(new Person("Akshay","Kumar"));
 person.add(new Person("Prashant","Gupta"));

/* Sorting- sort method will use compareTo(Object obj) override implementation for natural sorting
 */

Collections.sort(person);

for (Person p:person){
 System.out.println(p.getFirstname()+" "+p.getLastname());
 }
 }
 }
/* Result:

Akshay Kumar
Prashant Gupta
Rajiv Srivastava


*/
/* ———— Sorting thru Comparator Interface (Custom Sorting)
* Implements mandatory Comparator interface
*/

Comparator:


public class <b>MyCustomComparator</b> implements Comparator<Object> {

public int compare(Object obj1, Object obj2){
Empl p1=(Empl) obj1;
Empl p2=(Empl) obj2;

String p1name=p1.getFirstname()+" "+p1.getLastname();
String p2name=p2.getFirstname()+" "+p2.getLastname();

return p1name.compareTo(p2name);
}

public static void main(String[] args) {
List <Empl> plist= new ArrayList<Empl>();

plist.add(new Empl("Arvind","Upadhyay"));
plist.add(new Empl("Arvind","Tendulkar"));
plist.add(new Empl("Arvind","Kejriwal"));

Collections.sort(plist, new MyCustomComparator());

for(Empl p:plist){
System.out.println(p.firstname+ " "+p.getLastname());
}
}

}

class Empl{

String firstname;
String lastname;
public Empl(String firstname, String lastname) {
super();
this.firstname = firstname;
this.lastname = lastname;
}

public String getFirstname() {
return firstname;
}
public String getLastname() {
return lastname;
}
}

/* Output:
* Arvind Kejriwal
Arvind Tendulkar
Arvind Tripathi */

Advertisements

One thought on “Comparable and Comparator example

  1. I read a lot of interesting articles here. Probably you spend a lot of time writing, i know how to save you a lot of work, there is an online tool that creates
    unique, SEO friendly articles in minutes, just type in google – laranitas free content source

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s