comparable vs comparator
Comparable In java :- Basically
Comparable is an interface in java.lang package which is used to order the
objects of user-defined class. This interface has only one method named
compareTo(Object).
1.Sudent.java Class :-
public class Student
implements
Comparable<Student> {
public int rollno;
int
standard;
public String
name;
String address;
public int age;
public
Student(int rollno,
int
standard, String name, String address, int age) {
this.rollno
= rollno;
this.standard
= standard;
this.name =
name;
this.address
= address;
this.age =
age;
}
public int getRollno() {
return rollno;
}
public void
setRollno(int rollno)
{
this.rollno
= rollno;
}
public int getStandard() {
return
standard;
}
public void
setStandard(int
standard) {
this.standard
= standard;
}
public String
getName() {
return name;
}
public void
setName(String name) {
this.name =
name;
}
public String
getAddress() {
return
address;
}
public void
setAddress(String address) {
this.address
= address;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age =
age;
}
public int compareTo(Student st) {
if (age == st.age)
return 0;
else if (age > st.age)
return 1;
else
return -1;
}
}
2. ComparableTest.java Class
import
java.util.ArrayList;
import
java.util.Collections;
public class
ComparableTest {
public static void
main(String[] args) {
// TODO
Auto-generated method stub
ArrayList<Student> al = new
ArrayList<Student>();
al.add(new Student(101, 5, "Vijay", "Mumbai", 23));
al.add(new Student(102, 7, "Ajay", "Pune", 27));
al.add(new Student(103, 6, "Jai", "Nashik", 21));
Collections.sort(al);
for
(Student st : al) {
System.out.println(st.rollno + "
" + st.name + "
" + st.age);
}
}
}
Output
:-
103
Jai 21
101
Vijay 23
102
Ajay 27
These are some another examples:
package
comparator;
public class Student
{
int rollno;
int standard;
String name;
String address;
int age;
Student(int rollno, String
name, int age) {
this.rollno = rollno;
this.name = name;
this.age = age;
}
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 int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
package
comparator;
import
java.util.ArrayList;
import
java.util.Collections;
import
java.util.Iterator;
public class
ComparatorTest {
public static void
main(String[] args) {
// TODO
Auto-generated method stub
ArrayList al = new ArrayList();
al.add(new Student(101, "Vijay", 23));
al.add(new Student(102, "Ajay", 27));
al.add(new Student(103, "Jai", 21));
al.add(new Student(104, "sanket", 21));
al.add(new Student(105, "santosh", 21));
al.add(new Student(106, "manish", 21));
al.add(new Student(107, "kanan", 21));
al.add(new Student(108, "pravin", 21));
al.add(new Student(109, "rishabh", 21));
al.add(new Student(110, "santosh", 21));
al.add(new Student(111, "dev", 21));
al.add(new Student(112, "rahul", 21));
al.add(new Student(113, "Jai", 21));
al.add(new Student(114, "deepak", 21));
System.err.println("Sorting
by Name...");
Collections.sort(al, new NameComparator());
Iterator itr = al.iterator();
while (itr.hasNext())
{
Student st =
(Student) itr.next();
System.out.println(st.rollno + "
" + st.name + "
" + st.age);
}
System.err.println("sorting
by age...");
Collections.sort(al, new AgeComparator());
Iterator itr2 = al.iterator();
while (itr2.hasNext())
{
Student st =
(Student) itr2.next();
System.out.println(st.rollno + "
" + st.name + "
" + st.age);
}
}
}
package
comparator;
import
java.util.Comparator;
public class
AgeComparator implements
Comparator<Student> {
@Override
public int compare(Student o1,
Student o2) {
Student s1 = (Student) o1;
Student s2 = (Student) o2;
if (s1.age == s2.age)
return 0;
else if (s1.age > s2.age)
return 1;
else
return -1;
}
}
package
comparator;
import
java.util.Comparator;
public class
NameComparator implements
Comparator<Student> {
@Override
public int compare(Student o1,
Student o2) {
Student s1 = (Student) o1;
Student s2 = (Student) o2;
return s1.name.compareTo(s2.name);
}
}
public class
DogDoorSimulator {
public static void
main(String[] args) {
DogDoor door = new DogDoor();
door.addAllowedBark(new Bark("rowlf"));
door.addAllowedBark(new Bark("rooowlf"));
door.addAllowedBark(new Bark("rawlf"));
door.addAllowedBark(new Bark("woof"));
BarkRecognizer recognizer = new BarkRecognizer(door);
Remote remote = new Remote(door);
System.out.println("Bruce
starts barking.");
recognizer.recognize(new Bark("Rowlf"));
System.out.println("\nBruce
has gone outside...");
try {
Thread.currentThread().sleep(10000);
} catch
(InterruptedException e) {
}
System.out.println("\nBruce
all done...");
System.out.println("...but
he's stuck outside!");
// Simulate the hardware hearing a
bark (not Bruce!)
Bark smallDogBark = new Bark("yip");
System.out.println("A
small dog starts barking.");
recognizer.recognize(smallDogBark);
try {
Thread.currentThread().sleep(5000);
} catch
(InterruptedException e) {
}
// Simulate the hardware hearing a
bark again
System.out.println("\nBruce
starts barking.");
recognizer.recognize(new Bark("Rowlf"));
System.out.println("\nBruce's
back inside...");
}
}
No comments:
Post a Comment