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.

Syntax:

tupleObject1.compareTo(tupleObject2)

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:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Below is a Java program to demonstrate
// use of compareTo() method
  
import java.util.*;
import org.javatuples.Unit;
  
class GfG {
    public static void main(String[] args)
    {
        // Using with() method to instantiate unit object
        Unit<String> unit1 = Unit.with("a");
  
        // Using with() method to instantiate unit object
        Unit<String> unit2 = Unit.with("z");
  
        // Using compareTo()
        int result = unit1.compareTo(unit2);
  
        System.out.println(unit1.compareTo(unit2));
    }
}

chevron_right


Output:

-25

Explanation:
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:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Below is a Java program to demonstrate
// use of compareTo() method
  
import java.util.*;
import org.javatuples.Unit;
  
class GfG {
    public static void main(String[] args)
    {
        // Using with() method to instantiate unit object
        Unit<String> unit1 = Unit.with("Geeks");
  
        // Using with() method to instantiate unit object
        Unit<String> unit2 = Unit.with("Geeks");
  
        // Using compareTo()
        int result = unit1.compareTo(unit2);
  
        System.out.println(unit1.compareTo(unit2));
    }
}

chevron_right


Output:

0

Explanation:
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:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Below is a Java program to demonstrate
// use of compareTo() method
  
import java.util.*;
import org.javatuples.Unit;
  
class GfG {
    public static void main(String[] args)
    {
        // Using with() method to instantiate unit object
        Unit<String> unit1 = Unit.with("z");
  
        // Using with() method to instantiate unit object
        Unit<String> unit2 = Unit.with("A");
  
        // Using compareTo()
        int result = unit1.compareTo(unit2);
  
        System.out.println(unit1.compareTo(unit2));
    }
}

chevron_right


Output:

57

Explanation:
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.



My Personal Notes arrow_drop_up

Technical Content Engineer at GeeksForGeeks

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.