Thursday, October 9, 2025
HomeLanguagesJavaJavaTuples compareTo() method

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:




// 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.

Dominic
Dominichttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Dominic
32347 POSTS0 COMMENTS
Milvus
87 POSTS0 COMMENTS
Nango Kala
6715 POSTS0 COMMENTS
Nicole Veronica
11878 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11941 POSTS0 COMMENTS
Shaida Kate Naidoo
6837 POSTS0 COMMENTS
Ted Musemwa
7095 POSTS0 COMMENTS
Thapelo Manthata
6791 POSTS0 COMMENTS
Umr Jansen
6791 POSTS0 COMMENTS