Previously we have discussed about
Here we learn about Comparator Interface In Java, but we must also know What is Collections Framework In Java ? and What is Collection Interface In Java ?
Interface Comparator<T>
Type Parameters:
T - the type of objects that may be compared by this comparator
All Known Implementing Classes:
Collator, RuleBasedCollator
Declaration of Comparator:
public interface Comparator<T>
Comparator is a comparison function, which imposes a total ordering on some collection of objects.
This interface is belongs to java.util package and contains 2 methods compare(Object obj1,Object obj2) and equals(Object element).
Comparator compare() method returns zero if the objects are equal.It returns a positive value if obj1 is greater than obj2. Otherwise, a negative value is returned.This method also throw a ClassCastException if the types of the objects are not compatible for comparison.
Comparators provides multiple sorting sequence i.e. you can sort the elements on the basis of any data member.
Comparators can also be used to control the order of certain data structures (such as sorted sets or sorted maps), or to provide an ordering for collections of objects that don't have a natural ordering.
The ordering imposed by a comparator c on a set of elements S is said to be consistent with equals if and only if c.compare(e1, e2)==0 has the same boolean value as e1.equals(e2) for every e1 and e2 in S.
It is generally a good idea for comparators to also implement java.io.Serializable, as they may be used as ordering methods in serializable data structures (like TreeSet, TreeMap).
From many years, the preceding two methods were the only methods defined by Comparator. With the release of JDK 8, the situation has dramatically changed. JDK 8 adds significant new functionality to Comparator through the use of default and static interface methods.
This interface is a member of the Java Collections Framework.
Method Summary of Comparator:
Modifier and Method Name
|
Description
|
int compare(T o1, T o2)
|
Compares its two arguments for order.
|
boolean equals(Object obj)
|
Indicates whether some other object is "equal to" this
comparator.
|
Example of Comparator:
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 | import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; class Users { private String name; private int age; 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; } } class AgeComparator implements Comparator<Users>{ @Override public int compare(Users o1, Users o2) { return ((o1.getAge() == o2.getAge()) ? 0 : (o1.getAge() > o2.getAge()) ? 1 : -1); } } class NameComparator implements Comparator<Users>{ @Override public int compare(Users o1, Users o2) { return o1.getName().compareTo(o2.getName()); } } public class ComparatorInterfaceExample { public static void main(String[] args) { Users u = new Users(); u.setName("Vijay"); u.setAge(15); Users u1 = new Users(); u1.setName("Ajay"); u1.setAge(17); Users u2 = new Users(); u2.setName("Peter"); u2.setAge(12); ArrayList<Users> list = new ArrayList<Users>(); list.add(u); list.add(u1); list.add(u2); System.out.println("Sorting by Age :"); Collections.sort(list, new AgeComparator()); for(Users us : list){ System.out.println(us.getName()+"\t"+us.getAge()); } System.out.println("\nSorting by Name :"); Collections.sort(list, new NameComparator()); for(Users us : list){ System.out.println(us.getName()+"\t"+us.getAge()); } } } |
Output:
Blog Author - Pushkar Khosla,
Software Developer by Profession with 3.0 Yrs of Experience , through this blog i'am sharing my industrial Java Knowledge to entire world. For any question or query any one can comment below or mail me at pushkar.itsitm52@gmail.com.
This blog is all about to learn Core Java ,Interview Programs and Coding tricks to polish your Java Knowledge. If you like the content of this blog please share this with your friends.
Software Developer by Profession with 3.0 Yrs of Experience , through this blog i'am sharing my industrial Java Knowledge to entire world. For any question or query any one can comment below or mail me at pushkar.itsitm52@gmail.com.
This blog is all about to learn Core Java ,Interview Programs and Coding tricks to polish your Java Knowledge. If you like the content of this blog please share this with your friends.
Java I/O Tutorial

No comments:
Post a Comment