IntBuffer compareTo() method in Java
Last Updated :
06 Sep, 2022
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
Similar Reads
Java Integer compareTo() method The compareTo() method of Integer class of java.lang package compares two Integer objects numerically and returns the value 0 if this Integer is equal to the argument Integer; a value less than 0 if this Integer is numerically less than the argument Integer; and a value greater than 0 if this Intege
2 min read
Java Integer compare() method The compare() method of Integer class of java.lang package compares two integer values (x, y) given as a parameter and returns the value zero if (x==y), if (x < y) then it returns a value less than zero and if (x > y) then it returns a value greater than zero. Syntax : public static int compar
2 min read
IntBuffer equals() method in Java The equals() method of java.nio.IntBuffer Class is used to check whether or not the given buffer is equal to another object. Two int buffers are equal if, and only if, They have the same element type, They have the same number of remaining elements, and The two sequences of remaining elements, consi
4 min read
Java Integer compareUnsigned() method The compareUnsigned() method of the Integer class of java.lang package compares two integer values (x, y) given as a parameter, treating the values as unsigned and returns the value zero if (x==y), if (x < y) then it returns a value less than zero, and if (x > y) then it returns a value greate
2 min read
FloatBuffer compareTo() method in Java With Examples The compareTo() method of java.nio.FloatBuffer class is used to compare one buffer to another. Two float 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 float
4 min read