Thursday, 27 July 2023

Comparable vs Comparator

Comparable

  • It present in Java.lang Package.
  • It Contains only one Method.
  • Comparable can be used for natural or default sorting ordering.
        compareTo()

Method
  • public int compareTo(Object obj1,obj2)
obj1.compareTo(obj2)
  • Return(-ve), iff obj1 has to come before obj2.
  • Return(+ve), iff obj1 has to come after obj2.
  • Return(0),  iff obj1 & obj2 are equal.
foreg:
  • System.out.println("A".compareTo("z")); 
Return value is Negative/(-ve).
  • System.out.println("z".compareTo("k")); 
Return value is positivetive/(+ve)
  • System.out.println("A".compareTo("A")); 
Return value is Zero/(0)
  • System.out.println("A".compareTo("null"));
Return value is Null Pointer Exception.

Object1:-The Object, which is to be Inserted.
Object2:-The Object, which is already Inserted. 

If We are Depending on Default Natural Sorting Order while adding object in to the TreeSet JVM will call (compareTo) method.

Note:
  • If Default Natural Sorting Order not available or if we are not satisfied with Default Natural Sorting Order, than we can go for Customizer Sorting by using Comparator.
Comparator
  • It present in java.util.Package.
  • Comparator can be used for custom ordering.
    • It defines Two Methods. 
    1. compare() 
    2. equals()
Method
  1. public int compare(object obj1, obj2)
obj1.compareTo(obj2)
  • Return(-ve), iff obj1 has to come before obj2.
  • Return(+ve), iff obj1 has to come after obj2.
  • Return(0),  iff obj1 & obj2 are equal.
  2.public boolean equals(obj)
  • Whenever we are implementing Comparator interface compulsory we should provide implementation only for compare method.
  • we are not required to provide implementation for equals method .Because it is already available to our class from object class through Inheritance. 
For eg of Comparator
  • Write a program To Insert Objects into the TreeSet, Where the Sorting order is Descending Order


Output is


  • If we are not Passing comparator Object than internally JVM will call comparatorTo method, which is meant for Default Natural Sorting Order, In this case the output is 
  • [0,5,10,15,20]
  • If we are passing compare object than, JVM will call compare method which meant for Customizer Sorting in this case output is 
  • [20,15,10.5,0]
Various implementation of compare method
for eg:    Main class
       






result is Ascending Order

  • when you return I1(-ve) & I2(+ve)



Result is Descending 

  • when you return I2(-ve) & I1(+ve
Result is Ascending 



  • when you return -1

Result is [Rverse of Inversion Order]
  • when you return +1
Result is [Insertion Order]


  • when you return 0


Result is[Only First element will be insertion order all remaining consider as Duplicates]



Difference B/w Comparable & Comparator


No comments:

Post a Comment

Servlet in AEM/Sling Servlet

  What is Servlet? A servlet is a Java class that runs on the server side where the application resides. It acts as a middle layer between t...