The compareTo() method of java.nio.IntBuffer class is used to compare one buffer to another. Two int buffers are compared by comparing their sequences of remaining elements lexicographically, without regard to the starting position of each sequence within its corresponding buffer. Pairs of int elements are compared as if by invoking Int.compare(int, int), except that -0 and 0 are considered to be equal. Int.NaN is considered by this method to be equal to itself and greater than all other int values (including Int.POSITIVE_INFINITY). A int buffer is not comparable to any other type of object.
Syntax :
public int compareTo(IntBuffer that)
Parameter: This method takes a intbuffer object as a parameter with which this buffer will be compared.
Return Value: This method returns a negative integer, zero, or a positive integer as this buffer is less than, equal to, or greater than the given buffer.
Below program illustrates the compareTo() method:
Examples 1: When both IntBuffer are equal.
// Java program to demonstrate // compareTo() method import java.nio.*; import java.util.*; public class GFG { public static void main(String[] args) { // Declaring the capacity of the ib int capacity1 = 3; // Creating the IntBuffer try { // creating object of Intbuffer ib // and allocating size capacity IntBuffer ib = IntBuffer.allocate(capacity1); // putting the value in ib ib.put(9); ib.put(7); ib.put(4); // revind the Int buffer ib.rewind(); // print the IntBuffer System.out.println("IntBuffer ib: " + Arrays.toString(ib.array())); // creating object of Intbuffer ib1 // and allocating size capacity IntBuffer ib1 = IntBuffer.allocate(capacity1); // putting the value in ib1 ib1.put(9); ib1.put(7); ib1.put(4); // revind the Int buffer ib1.rewind(); // print the IntBuffer System.out.println("IntBuffer ib1: " + Arrays.toString(ib1.array())); // compare both buffer and store the value into integer int i = ib.compareTo(ib1); // if else condition if (i == 0) System.out.println("\nBoth buffer are lexicographically equal"); else if (i >= 0) System.out.println("\nib is lexicographically greater than ib1"); else System.out.println("\nib is lexicographically less than ib1"); } catch (IllegalArgumentException e) { System.out.println("Exception throws : " + e); } catch (ReadOnlyBufferException e) { System.out.println("Exception throws : " + e); } } } |
IntBuffer ib: [9, 7, 4] IntBuffer ib1: [9, 7, 4] Both buffer are lexicographically equal
Examples 2: When this IntBuffer is greater than the passed IntBuffer
// Java program to demonstrate // compareTo() method import java.nio.*; import java.util.*; public class GFG { public static void main(String[] args) { // Declaring the capacity of the ib int capacity1 = 3; // Creating the IntBuffer try { // creating object of Intbuffer ib // and allocating size capacity IntBuffer ib = IntBuffer.allocate(capacity1); // putting the value in ib ib.put(9); ib.put(7); ib.put(4); // revind the Int buffer ib.rewind(); // print the IntBuffer System.out.println("IntBuffer ib: " + Arrays.toString(ib.array())); // creating object of Intbuffer ib1 // and allocating size capacity IntBuffer ib1 = IntBuffer.allocate(capacity1); // putting the value in ib1 ib1.put(8); ib1.put(7); ib1.put(4); // revind the Int buffer ib1.rewind(); // print the IntBuffer System.out.println("IntBuffer ib1: " + Arrays.toString(ib1.array())); // compare both buffer and store the value into integer int i = ib.compareTo(ib1); // if else condition if (i == 0) System.out.println("\nBoth buffer are lexicographically equal"); else if (i >= 0) System.out.println("\nib is lexicographically greater than ib1"); else System.out.println("\nib is lexicographically less than ib1"); } catch (IllegalArgumentException e) { System.out.println("Exception throws : " + e); } catch (ReadOnlyBufferException e) { System.out.println("Exception throws : " + e); } } } |
IntBuffer ib: [9, 7, 4] IntBuffer ib1: [8, 7, 4] ib is lexicographically greater than ib1
Example 3: When this IntBuffer is less than the passed IntBuffer.
// Java program to demonstrate // compareTo() method import java.nio.*; import java.util.*; public class GFG { public static void main(String[] args) { // Declaring the capacity of the ib int capacity1 = 3; // Creating the IntBuffer try { // creating object of Intbuffer ib // and allocating size capacity IntBuffer ib = IntBuffer.allocate(capacity1); // putting the value in ib ib.put(8); ib.put(7); ib.put(4); // revind the Int buffer ib.rewind(); // print the IntBuffer System.out.println("IntBuffer ib: " + Arrays.toString(ib.array())); // creating object of Intbuffer ib1 // and allocating size capacity IntBuffer ib1 = IntBuffer.allocate(capacity1); // putting the value in ib1 ib1.put(9); ib1.put(7); ib1.put(4); // revind the Int buffer ib1.rewind(); // print the IntBuffer System.out.println("IntBuffer ib1: " + Arrays.toString(ib1.array())); // compare both buffer and store the value into integer int i = ib.compareTo(ib1); // if else condition if (i == 0) System.out.println("\nBoth buffer are lexicographically equal"); else if (i >= 0) System.out.println("\nib is lexicographically greater than ib1"); else System.out.println("\nib is lexicographically less than ib1"); } catch (IllegalArgumentException e) { System.out.println("Exception throws : " + e); } catch (ReadOnlyBufferException e) { System.out.println("Exception throws : " + e); } } } |
IntBuffer ib: [8, 7, 4] IntBuffer ib1: [9, 7, 4] ib is lexicographically less than ib1
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
Recommended Posts:
- IntBuffer asReadOnlyBuffer() method in Java
- IntBuffer arrayOffset() method in Java
- IntBuffer wrap() method in Java
- IntBuffer equals() method in Java
- IntBuffer allocate() method in Java
- IntBuffer slice() method in Java
- IntBuffer array() method in Java
- IntBuffer compact() method in Java
- IntBuffer hasArray() method in Java
- IntBuffer rewind() method in Java with Examples
- IntBuffer mark() method in Java with Examples
- IntBuffer limit() method in Java with Examples
- IntBuffer flip() method in Java with Examples
- IntBuffer clear() method in Java with Examples
- IntBuffer duplicate() method in Java with Examples
- BigInteger compareTo() Method in Java
- CharBuffer compareTo() method in Java
- YearMonth compareTo() method in Java
- LocalDate compareTo() method in Java
- ZoneOffsetTransition compareTo() method in Java with Example
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.

