Monday, 23 July 2018

SE - 27 - Comparator and Comparable


Comparable :
i) Interface that needs to be implemented:  java.lang.Comparable
ii) A comparable object compares its own instance with other class object instance
iii) It has only one method , Syntax : int obj1.compareTo(obj2)
iv) Returns : Negative , if obj1< obj2
                     Zero ,  if obj1 = obj2
                     Positive ,  if obj1 > obj2
v) Default natural sorting order
vi) All wrapper and string classes implements comparable


Comparator:
i) Interface that needs to be implemented:  java.util.Comparator
ii) An object of comparator compares objects of two different class instances
iii) It contains 2 methods :
      Syntax : int compare(obj1, obj2) , Int equals(obj1 , obj2)
iv) Returns : Negative , if obj1< obj2
                     Zero ,  if obj1 = obj2
                     Positive ,  if obj1 > obj2
v) Customised sorting order
vi) Collator and ruleBased collator implements comparator.

No comments:

Post a Comment