IntBuffer compareTo() method in Java

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.

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Output:

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

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Output:

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.

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Output:

IntBuffer ib: [8, 7, 4]
IntBuffer ib1: [9, 7, 4]

ib is lexicographically less than ib1


My Personal Notes arrow_drop_up

Stepping steps of my ownself

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.