- It is inside java.lang package.
- The class whose elements need to be sorted must implement this interface and override the compareTo(Object obj) method.
- The sort is done using either Collection.sort(List<T> list) or Arrays.sort(Array[] array)
package com.arsoft.tests;
public class Student implements Comparable<Student>{ //class is implementing Comparable
private String name;
private float marks;
public Student (String name, float marks){
this.name = name;
this.marks = marks;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public float getMarks() {
return marks;
}
public void setMarks(float marks) {
this.marks = marks;
}
public int compareTo(Student student){ //Method needs to be overridden
return this.name.compareTo(student.getName());
}
}
package com.arsoft.tests;
import java.util.ArrayList;
import java.util.Collections;
public class StudentDemo {
public static void main(String args[]){
ArrayList<Student> students = new ArrayList<Student>();
Student student = new Student("Abhishek", 95);
students.add(student);
student = new Student("Anshul",93);
students.add(student);
student = new Student("Akhil",86);
students.add(student);
student = new Student("Rajeev",99);
students.add(student);
student = new Student("Ruchika", 67);
students.add(student);
for(Student st : students) {
System.out.println(st.getMarks() + " : " + st.getName());
}
System.out.println("##################################");
Collections.sort(students);
for(Student st : students) {
System.out.println(st.getMarks() + " : " + st.getName());
}
}
}
Comparator:-
- It is inside java.util package.
- The class whose elements need to be sorted must NOT implement this interface instead we can create a separate class implementing this interface and the override the compare(Object obj, Object obj1) method.
- The sort is done using either Collection.sort(List<T> list, Comparator comparator) or Arrays.sort(Array[] array, Comparator comparator)
import java.util.Comparator;
public class NewStudent { //Class is not implementing Comparator
private String name;
private float marks;
public NewStudent (String name, float marks){
this.name = name;
this.marks = marks;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public float getMarks() {
return marks;
}
public void setMarks(float marks) {
this.marks = marks;
}
}
package com.arsoft.tests;
import java.util.Comparator;
public class NameComparator implements Comparator<NewStudent>{ //New comparator which //implements comparator is created
@Override
public int compare(NewStudent student1, NewStudent student2) { //Method is overridden
return student1.getName().compareTo(student2.getName());
}
}
package com.arsoft.tests;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
public class NameStudentDemo {
public static void main(String args[]){
ArrayList<NewStudent> students = new ArrayList<NewStudent>();
NewStudent student = new NewStudent("Abhishek", 95);
students.add(student);
student = new NewStudent("Anshul",93);
students.add(student);
student = new NewStudent("Akhil",86);
students.add(student);
student = new NewStudent("Rajeev",99);
students.add(student);
student = new NewStudent("Ruchika", 67);
students.add(student);
for(NewStudent st : students) {
System.out.println(st.getMarks() + " : " + st.getName());
}
System.out.println("##################################");
NameComparator nc = new NameComparator();
Collections.sort(students, nc); //an object of custom comparator class is being used in sorting
for(NewStudent st : students) {
System.out.println(st.getMarks() + " : " + st.getName());
}
}
}
No comments:
Post a Comment