Open In App

JavaTuples compareTo() method

Improve
Improve
Like Article
Like
Save
Share
Report

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:




// 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));
    }
}


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:




// 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));
    }
}


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:




// 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));
    }
}


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.



Last Updated : 23 Aug, 2018
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads