Open In App

IntBuffer compareTo() method in Java

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Share
Report issue
Report

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




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




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




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


Last Updated : 06 Sep, 2022
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads