Skip to content
Related Articles

Related Articles

IntBuffer compareTo() method in Java
  • Last Updated : 20 Sep, 2018

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




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




// 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);
        }
    }
}
Output:
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 Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

My Personal Notes arrow_drop_up
Recommended Articles
Page :