• September 3, 2014
  • Core Java
  • Comments Off on Comparable interface in java

Comparable interface in java

Comparable interface:

Comparable interface is defined in java.lang package. It has only one method namedcompareTo(Object o).Itis used to implement natural ordering of collection elements. String and wrapper classes implements Comparable interface.

Note: We can sort the elements based on single property only. Suppose we have Student class elements with name, class and rollNo as properties then by using comparable interface we can sort student objects based on single property only either by name or some other one.

Method of Comparable interface:

 compareTo(Object obj): It is used to compare this object with the specified object. It returns +ve integer if this object is greater than the specified object, 0 if this object is equals to the specified object and –ve integer if this object is less than the specified object.

Syntax: public int compareTo(Object obj).

Sorting example using Comparable interface:

Student.java

/**
 * This class represents a Student and 
 * implements Comparable.
 * @author javawithease
 */
public class Student implements Comparable{
	//data members
	private String name;
	private String rollNo;
	private int age;
 
	//no-argument constructor
	public Student(){
 
	}
 
	//argument cnstructor
	public Student(String name, String rollNo, int age){
		this.name = name;
		this.rollNo = rollNo;
		this.age = age;
	}
 
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getRollNo() {
		return rollNo;
	}
	public void setRollNo(String rollNo) {
		this.rollNo = rollNo;
	}
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}
 
	//Method for sorting logic
	public int compareTo(Object obj){  
		Student student=(Student)obj;  
		return (this.name).compareTo(student.name);  
	}  
 
}

Test.java

/**
 * This class is used to show the sorting functionality.
 * @author javawithease
 */
public class Test {
	public static void main(String args[]){
		ArrayList studentList = new ArrayList();
 
		studentList.add(new Student("Sandy", "MCA/07/06", 28));
		studentList.add(new Student("Roxy", "MCA/07/32", 28));
		studentList.add(new Student("Sunil", "MCA/07/15", 27));
		studentList.add(new Student("Munish", "MCA/07/04", 27));
 
		Collections.sort(studentList);
 
		Iterator iterator=studentList.iterator();  
		while(iterator.hasNext()){  
			Student student=(Student)iterator.next();  
			System.out.println("Name: " + student.getName()+
				  ", " + "RollNo: "+student.getRollNo()+
					", Age: "+student.getAge());  
		} 		
	}
}

Output:

Name: Munish, RollNo: MCA/07/04, Age: 27
Name: Roxy, RollNo: MCA/07/32, Age: 28
Name: Sandy, RollNo: MCA/07/06, Age: 28
Name: Sunil, RollNo: MCA/07/15, Age: 27

Download this example.
 
Next Topic: Comparator interface in java with example.
Previous Topic: Sorting in java with example.

Related Topics:

Collection framework in java.
Collection interfaces in java.
Collection interface in java with example.
Set interface in java with example.
SortedSet interface in java with example.
List interface in java with example.
Map interface in java with example.
Daemon thread in java in java with example.
SortedMap interface in java with example.
Queue interface in java with example.
Deque interface in java with example.
Enumeration interface in java with example.
Collection classes in java with example.
HashSet in java with example.
LinkedHashSet in java with example.
TreeSet in java with example.
ArrayList in java with example.
LinkedList in java with example.
HashMap in java with example.
LinkedHashMap in java with example.
TreeMap in java with example.
PriorityQueue in java with example.
ArrayDeque in java with example.
Abstract classes in collection framework in java with example.
Sorting in java with example.
Comparable interface in java with example.
Comparator interface in java with example.
Properties class in java with example.
Hashtable in java with example.
ListIterator interface in java with example.

 


Comments are closed.