Comparable Interface
Comparable is an interface in java which is available in java.lang package. Comparable interface is used to sort or order the objects of a user-defined class.
Comparable interface imposes a total ordering on the objects of each class that implements it. This ordering is referred to as the class's natural ordering and the class's compareTo method is referred to as its natural comparison method.
Comparable interface basically provides single sorting sequence i.e you can sort the elements on the basis of single data member such as name, age, salary, etc.
Comparable interface contains only one method is called compareTo(object) method.
Comparable interface provides natural sorting that is the reason all wrapper classes and String class implementing this interface and overrides compareTo(object) method.
Comparable interface imposes a total ordering on the objects of each class that implements it. This ordering is referred to as the class's natural ordering and the class's compareTo method is referred to as its natural comparison method.
Comparable interface basically provides single sorting sequence i.e you can sort the elements on the basis of single data member such as name, age, salary, etc.
Comparable interface contains only one method is called compareTo(object) method.
Comparable interface provides natural sorting that is the reason all wrapper classes and String class implementing this interface and overrides compareTo(object) method.
Method of Comparable Interface
There is a single method of the Comparable interface.
public int compareTo(Object obj) : This method compares this object with the specified object for order and returns a negative integer, zero or a positive integer as this object is less than, or equal to and greater than the specified object.
This method throws some exception like
java.lang.NullPointerException - if the specified object is null.
java.lang.ClassCastException - if the specified object's type prevents it from being compared to this object.
By the help of this method, we can sort the elements of
This method throws some exception like
java.lang.NullPointerException - if the specified object is null.
java.lang.ClassCastException - if the specified object's type prevents it from being compared to this object.
By the help of this method, we can sort the elements of
- String objects.
- user-defined class objects.
- wrapper class objects.
Collections Class
Collections is a class in java, It is not a collection(interface), it is a collections(class) which provides us static method so that we can easily sort the elements of collections. If the elements of collection is a type of Set or Map then we will sort these elements easily by using treeSet and treeMap class but we cannot sort the elements of List if we want to sort the elements of List type, We have to use method of Collections's class so that we can easily sort the elements of List type.
Collections class extends only super class i.e Object class.
The methods of this class all throw a NullPointerException, if the collections or class objects provide them are null.
Collections class extends only super class i.e Object class.
The methods of this class all throw a NullPointerException, if the collections or class objects provide them are null.
Method of Collections Class
public void sort(List list) : This method is used to sort the elements of List and List elements must be of comparable type.
Java Comparable Example
This is a simple comparable example where we sort the list elements on the basis of age.
File1 : Student.java
class Student implements Comparable<Student>
{
int rollno;
String name;
int age;
Student(int rollno, String name, int age)
{
this.rollno=rollno;
this.name=name;
this.age=age;
}
//using compareTo method
public int compareTo(Student st)
{
if(age == st.age)
return 0;
else if(age>st.age)
return 1;
else
return -1;
}
}
File2 : SortExample.java
import java.util.*;
class SortExample
{
public static void main(String args[])
{
ArrayList<Student> al = new ArrayList<Student>();
al.ad(new Student(1, "karan", 18));
al.ad(new Student(3, "amar", 17));
al.ad(new Student(5, "varun", 27));
al.ad(new Student(2, "ranbir", 50));
//using sort method of Collections class
Collections.sort(al);
for(Student st : al)
{
System.out.println(st.rollno+" "+st.name+" "+st.age);
}
}
}
output : 3 amar 17
1 karan 18
5 varun 27
2 ranbir 50
Read More:
Java Comparator Interface with Example.
Java Encapsulation Concept with Examples.
Java Abstraction Tutorial with Examples.
Java Collection Framework Tutorial.
Java Collections Class with Examples.
Java Interface Interview Questions.
In the above example, we sorted the elements on the basis of age.
Read More:
Java Comparator Interface with Example.
Java Encapsulation Concept with Examples.
Java Abstraction Tutorial with Examples.
Java Collection Framework Tutorial.
Java Collections Class with Examples.
Java Interface Interview Questions.
In the above example, we sorted the elements on the basis of age.
Thanks For Sharing. Especia Associates provides SEBI Valuation Consultants. Our SEBI valuation process is conducted by a team of professionals with years of experience in valuation. Our team of BSE/NSE impaneled analysts, chartered accountants, and SEBI registered merchant bankers are well acquainted with the regulations and requirements of the stock exchange. We have prepared hundreds of valuation reports and have assisted in determining the value of the company’s stock along with valuations of similar companies. if you need SEBI Valuation Consultant call at 9310165114 or visit us SEBI Valuation Consultants
ReplyDelete