Sponsored Links
Ad by Google
Friends, In our previous post we have seen java.util.Comparator tutorial, Here we are going to show you the implementation example of java.lang.Comparable interface. The Comparable interface is one of the very popular interface in java. It has a single public method
The sorting of element is only possible after the comparison of the elements, that is why Comparable interface has a compareTo(T o) method. The implemented class must have to provide the implementation of compareTo(T o) method, the compareTo method is almost same as equals(Object o) method of java.lang.Object class except that compareTo method provides the comparisons between the objects whereas equals method checks the equality of objects and return true/false. Once you implement Comparable interface means you are indicating that instance of this class is have a natural ordering.
Natural Ordering Examples:
Numeric natural order for Numbers like 1,2,3..4
Alphabetic natural order for String like "Alarm","Basket"..."Xyz"
and Chronological natural order for dates like 2014-01-01, 2014-02-02...2015-02-02
Lets see the implementation example of java.lang.Comparable interface,
Contact.java implements java.lang.Comparable interface
Here is MainContact.java class, which will sort the element of contactList using Collections.sort(contactList) and printing it on console. Sorting of contact element is done on the basis of emailId.
OUT PUT:
20 Basic core java interview questions here
What are the new features of java 7 here
10 JSP Servlet interview Questions here
Java Program to balanced the parenthesis with o(n) order here
public int compareTo(T o)
If you are planing to use your object in Collection and may needs to sort the elements of that Collection than you must have to implement java.lang.Comparable or java.util.Comparator interface, here we are going to implement Comparable interface. Although we have listed few key differences between java.lang.Comparable and java.util.Comparator interface.The sorting of element is only possible after the comparison of the elements, that is why Comparable interface has a compareTo(T o) method. The implemented class must have to provide the implementation of compareTo(T o) method, the compareTo method is almost same as equals(Object o) method of java.lang.Object class except that compareTo method provides the comparisons between the objects whereas equals method checks the equality of objects and return true/false. Once you implement Comparable interface means you are indicating that instance of this class is have a natural ordering.
Natural Ordering Examples:
Numeric natural order for Numbers like 1,2,3..4
Alphabetic natural order for String like "Alarm","Basket"..."Xyz"
and Chronological natural order for dates like 2014-01-01, 2014-02-02...2015-02-02
Lets see the implementation example of java.lang.Comparable interface,
Contact.java implements java.lang.Comparable interface
package com.javamakeuse.poc; public class Contact implements Comparable<Contact> { private String emailID; private String mobile; public Contact(String emailID, String mobile) { super(); this.emailID = emailID; this.mobile = mobile; } public String getEmailID() { return emailID; } public String getMobile() { return mobile; } @Override public int compareTo(Contact o) { return this.emailID.compareTo(o.getEmailID()); } @Override public String toString() { return "Contact [emailID=" + emailID + ", mobile=" + mobile + "]"; } }In Contact.java class, we have implemented Comparable interface and provided the implementation of compareTo(T o) method. The return type of compareTo method is int, so it will return negative integer, zero or positive integer value. Object that implements Comparable interface can also be used as key in a Map or Set.
Here is MainContact.java class, which will sort the element of contactList using Collections.sort(contactList) and printing it on console. Sorting of contact element is done on the basis of emailId.
package com.javamakeuse.poc; import java.util.ArrayList; import java.util.Collections; import java.util.List; public class MainContact { public static void main(String[] args) { List<Contact> contactList = getContacList(); System.out.println("Befor sorting ***"); for (Contact contact : contactList) { System.out.println(contact); } System.out.println("After sorting ***"); Collections.sort(contactList); for (Contact contact : contactList) { System.out.println(contact); } } public static List<Contact> getContacList() { List<Contact> contactList = new ArrayList<Contact>(); contactList.add(new Contact("1@me2.com", "1234567890")); contactList.add(new Contact("3@me2.com", "12345567890")); contactList.add(new Contact("2@me2.com", "1234456789")); return contactList; } }
OUT PUT:
Befor sorting *** Contact [emailID=1@me2.com, mobile=1234567890] Contact [emailID=3@me2.com, mobile=12345567890] Contact [emailID=2@me2.com, mobile=1234456789] After sorting *** Contact [emailID=1@me2.com, mobile=1234567890] Contact [emailID=2@me2.com, mobile=1234456789] Contact [emailID=3@me2.com, mobile=12345567890]
Popular java interview questions
15 Core java written test questions for experienced here20 Basic core java interview questions here
What are the new features of java 7 here
10 JSP Servlet interview Questions here
Java Program to balanced the parenthesis with o(n) order here
Sponsored Links
0 comments:
Post a Comment