Tuesday, 9 February 2016

Comprator and comprable in java

Comprable:-it is interface  which provides nature sorting.it present in java.lang.package. Comparable provides compareTo() method to sort elements.
Use of Comprable:
if we have only one single sorting criteria to sort the element than we can go for comprable.
example:-we can sort the collection on the basis of single element such as id or name or price etc.

Note:-) We can sort the list elements of Comparable type by Collections.sort(List) method.

Comprator:-it is also interface whitch provides custom sorting, it is present in java.util package.Comparator provides compare() method to sort elements.

Use Of Comprator:-if we have more than one sorting  criteria to sort the element than we go for comprable.
Example:- we can sort the collection on the basis of multiple elements such as id, name and price etc.

Note:We can sort the list elements of Comparator type by Collections.sort(List,Comparator) method.

No comments:

Post a Comment