In: Computer Science
Registry Java Programming
2) Interface Comparator:
The method for comparing two objects is written outside of the
class of the objects to be sorted. Several methods can be written
for comparing the objects according to different criteria.
Specifically, write three classes, DescriptionComparator,
FirstOccComparator,
and LastOccComparator that implement the interface
java.util.Comparator.
DescriptionComparator implements the interface Comparator.
The
method compare compares the description of two objects. It returns
a negative value if the description of the first object comes
before the description of the second object in the lexicographic
order, 0 if both descriptions are logically the same, and a
positive number if the description of the first object comes after
the description of the second object in the lexicographic
order;
FirstOccComparator implements the interface Comparator. It compares the start time of two events. It returns a negative value if the start time of the first event is before that of the second event, 0 if both start times are logically the same, and a positive number if the start time of the first event is after the start time of the second event;
LastOccComparator implements the interface Comparator. It compares the dates of the last recurrences of two events. It returns a negative value if the date of the last recurrence of the first event is before that of the last recurrence of the second event, 0 if the date of the last recurrence of both events is logically the same, and a positive number if the date the last recurrence of the first event is after the date of the last recurrence of the second event.
DescriptionComparator class
package comaprator;
import java.util.Comparator;
//comparator with description comparison
public class DescriptionComparator implements Comparator<MyObject>{
@Override
public int compare(MyObject o1, MyObject o2) {
return o1.description.compareTo(o2.description); //return -1,0 ,1 if string lexicographically
//less equal, and greater respectively
}
}
FirstOccComparator class
package comaprator;
import java.util.Comparator;
//comparator with event start time comparison
public class FirstOccComparator implements Comparator<MyObject>{
@Override
public int compare(MyObject o1, MyObject o2) {
if(o1.startTime.before(o1.startTime))
return -1;
if(o1.startTime.after(o2.startTime))
return 1;
return 0;
}
}
LastOccComparator class
package comaprator;
import java.util.Comparator;
//comparator with date of last occurrence
public class LastOccComparator implements Comparator<MyObject>{
@Override
public int compare(MyObject o1, MyObject o2) {
if(o1.lastOccurence.before(o2.lastOccurence))
return -1;
if(o1.lastOccurence.after(o2.lastOccurence))
return 1;
return 0;
}
}
MyObject class
package comaprator;
//object outside of the comparison class to compare
import java.sql.Timestamp;
import java.util.Date;
public class MyObject {
public MyObject(String description, Timestamp startTime, Date lastOccurence) {
super();
this.description = description;
this.startTime = startTime;
this.lastOccurence = lastOccurence;
}
String description;
Timestamp startTime;
Date lastOccurence;
@Override
public String toString() {
return "MyObject [description=" + description + ", startTime=" + startTime + ", lastOccurence=" + lastOccurence
+ "]\n";
}
}
Tester class
package comaprator;
//driver program to test
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
public class Tester {
public static void main(String[] args) {
MyObject m1=new MyObject("hello",new Timestamp(765432),new Date("13/06/2016"));
MyObject m2=new MyObject("wow",new Timestamp(60002),new Date("03/8/2020"));
MyObject m3=new MyObject("hi",new Timestamp(765432),new Date("13/06/1998"));
ArrayList<MyObject> l=new ArrayList<>();
l.add(m1);
l.add(m2);
l.add(m3);
System.out.println(l);
Collections.sort(l,new DescriptionComparator());
System.out.println(l);
l.clear(); //cleae list to have clean data
l.add(m1);
l.add(m2);
l.add(m3);
Collections.sort(l,new FirstOccComparator());
System.out.println(l);
l.clear();
l.add(m1);
l.add(m2);
l.add(m3);
Collections.sort(l,new LastOccComparator());
System.out.println(l);
}
}
output
[MyObject [description=hello, startTime=1970-01-01 05:42:45.432, lastOccurence=Fri Jan 06 00:00:00 IST 2017]
, MyObject [description=wow, startTime=1970-01-01 05:31:00.002, lastOccurence=Sun Mar 08 00:00:00 IST 2020]
, MyObject [description=hi, startTime=1970-01-01 05:42:45.432, lastOccurence=Wed Jan 06 00:00:00 IST 1999]
]
[MyObject [description=hello, startTime=1970-01-01 05:42:45.432, lastOccurence=Fri Jan 06 00:00:00 IST 2017]
, MyObject [description=hi, startTime=1970-01-01 05:42:45.432, lastOccurence=Wed Jan 06 00:00:00 IST 1999]
, MyObject [description=wow, startTime=1970-01-01 05:31:00.002, lastOccurence=Sun Mar 08 00:00:00 IST 2020]
]
[MyObject [description=hello, startTime=1970-01-01 05:42:45.432, lastOccurence=Fri Jan 06 00:00:00 IST 2017]
, MyObject [description=wow, startTime=1970-01-01 05:31:00.002, lastOccurence=Sun Mar 08 00:00:00 IST 2020]
, MyObject [description=hi, startTime=1970-01-01 05:42:45.432, lastOccurence=Wed Jan 06 00:00:00 IST 1999]
]
[MyObject [description=hi, startTime=1970-01-01 05:42:45.432, lastOccurence=Wed Jan 06 00:00:00 IST 1999]
, MyObject [description=hello, startTime=1970-01-01 05:42:45.432, lastOccurence=Fri Jan 06 00:00:00 IST 2017]
, MyObject [description=wow, startTime=1970-01-01 05:31:00.002, lastOccurence=Sun Mar 08 00:00:00 IST 2020]
]