JavaTuples compareTo() method
The compareTo() method in org.javatuples is used to compare the order of a Tuple object with the object passed as the parameter. This method can be used for any tuple class object of javatuples library. It returns the difference in the ASCII value of the 1st different element present in the passed object, from the called object.
Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.
Parameters: This method takes a mandatory parameter tupleObject2 which is the JavaTuple object to be compared.
Return Value: This method the difference in the ascii value of the 1st different element present in the passed object, from the called object. It might be negative, 0, or positive integer value.
Below programs illustrate the various ways to use compareTo() method:
Program 1: When compareTo() gives negative result:
The ascii value of ‘a’ is 97 while that of ‘z’ is 122. Hence a.compareTo(z) yielded -25 (=122-97) as the result.
Program 1: When compareTo() gives 0 as result:
Since “Geeks” is same as “Geeks”. Hence they are equal. So in this case Geeks.compareTo(Geeks) yielded 0.
Program 3: When compareTo() gives positive result:
The ascii value of ‘A’ is 65 while that of ‘z’ is 122. Hence z.compareTo(A) yielded 57 (=122-65) as the result.
Note: Similarly, it can be used with any other JavaTuple Class.