Comparable Interface
What is Comparable Interface in java collection
In my previous post TreeSet in Java Collection With Very Basic Example
I explained TreeSet class which stored Integer numbers. You have seen TreeSet stored Integer numbers in ordered way.
Now think about the scenario you want to store Object of your class into TreeSet. In my case I have a Student class and I have five objects of type Student, Now I want to store these objects in TreeSet.
The matter of discussion is we know that TreeSet store objects in ordered way. How TreeSet will come to know the order of Student type objects.Here the order might be according to student roll number, student name, student percentage etc.
We have to define the order by implementing the Comparable Interface.
Comparable Interface have a method compareTo() which compare one object to another object. It returns positive integer number if current object which invoked method is greater than object which is passed as parameter, negative integer if current object which invoked method is smaller than object which is passed as parameter,
and zero if both equal.
Suppose I want to store objects of Student type according to roll number, then I have to implement Comparable interface and override the compareTo method.
Please go thorough example.
Student.java
Student.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 | package com.javasuitor.bean; public class Student implements Comparable{ private int rollNo; private String name; private double percentage; public Student() { // TODO Auto-generated constructor stub } public Student(int rollNo, String name, double percentage) { super(); this.rollNo = rollNo; this.name = name; this.percentage = percentage; } public int getRollNo() { return rollNo; } public void setRollNo(int rollNo) { this.rollNo = rollNo; } public String getName() { return name; } public void setName(String name) { this.name = name; } public double getPercentage() { return percentage; } public void setPercentage(double percentage) { this.percentage = percentage; } @Override public boolean equals(Object object) { // we need to type cast Object into Student type. Student student = (Student)object; if(this.rollNo == student.rollNo) return true; else return false; } @Override public int hashCode() { // hash code will be roll number of student. return this.rollNo; } @Override public int compareTo(Object object) { Student s = (Student) object; return this.rollNo - s.rollNo; } }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 | package com.javasuitor.demo; import java.util.Iterator; import java.util.TreeSet; import com.javasuitor.bean.Student; public class StudentTreeSetDemo { public static void main(String[] args) { TreeSet<Student> studentSet = new TreeSet<Student>(); Student s1 = new Student(101,"Ashok",82.2); Student s2 = new Student(102,"Bibin",80.3); Student s3 = new Student(103,"Devang",76.0); Student s4 = new Student(104,"Sourabh",74.2); Student s5 = new Student(105,"Ravi",78.33); studentSet.add(s1); studentSet.add(s2); studentSet.add(s3); studentSet.add(s4); studentSet.add(s5); // Print Students details System.out.println("Students in student Set"); Iterator<Student> studentIterator = studentSet.iterator(); while (studentIterator.hasNext()) { Student s = studentIterator.next(); System.out.println("Roll No: "+s.getRollNo() +" Name: "+s.getName()+" Percentage: "+s.getPercentage()); } } } |
OutPut
You can see in output all objects are arranged according to roll number. I hope you understood the functionality of TreeSet.
Please feel free to ask any question.
Happy learning.
Well Explain examples and also visit
ReplyDeletehttp://www.javaproficiency.com/2015/11/how-to-sort-treeset-with-user-defined.html