What is the compareTo() method of the IntBuffer class in Java?

The compareTo() method of the IntBuffer class in Java lexicographically compares the sequences of two IntBuffer objects. The comparison between each pair of elements disregards the starting position of each sequence within their respective buffers.

The compareTo() method uses the conventional rules of integer comparison, except that 0-0 and 00 are considered equal, and NaN (Not-A-Number) is considered greater than all other integer values.

The process is illustrated below:

Note: An IntBuffer object can only be compared to another IntBuffer object, not any other type.

To use the compareTo() method, you need to import the IntBuffer class into your program, as shown below:

import java.nio.IntBuffer

The prototype of the compareTo() method is shown below:

public int compareTo(IntBuffer that)

Parameters

The compareTo() method takes a single mandatory parameter: the object to compare with the buffer.

Return value

The compareTo() method compares two IntBuffer objects and returns one of the following:

  • If the IntBuffer object used to invoke the method is less than the IntBuffer object provided as a parameter, then the compareTo() method returns a negative integer.
  • If the two IntBuffer objects are equal, then the compareTo() method returns 00.
  • If the IntBuffer object used to invoke the method is greater than the IntBuffer object provided as a parameter, then the compareTo() method returns a positive integer.

Example

The code below shows how the compareTo method works in Java:

import java.nio.*;
import java.util.*;
class equalsMethod {
public static void main(String[] args) {
// initialize IntBuffer instance
IntBuffer bufferOne = IntBuffer.allocate(3);
IntBuffer bufferTwo = IntBuffer.allocate(3);
IntBuffer bufferThree = IntBuffer.allocate(3);
IntBuffer bufferFour = IntBuffer.allocate(3);
IntBuffer bufferFive = IntBuffer.allocate(5);
int setOne[] = {2, 1, 6};
int setTwo[] = {2, 1, 4};
int setThree[] = {2, 3, 1};
int setFour[] = {2, 1, 6, 10, 18};
// add values to buffers
for(int i = 0; i < 5; i++)
{
if(i < 3)
{
bufferOne.put(setOne[i]);
bufferTwo.put(setOne[i]);
bufferThree.put(setTwo[i]);
bufferFour.put(setThree[i]);
}
bufferFive.put(setFour[i]);
}
// set starting position to index 0
bufferOne.rewind();
bufferTwo.rewind();
bufferThree.rewind();
bufferFour.rewind();
bufferFive.rewind();
// Print buffers
System.out.println("BufferOne is: " + Arrays.toString(bufferOne.array()));
System.out.println("BufferTwo is: " + Arrays.toString(bufferTwo.array()));
System.out.println("BufferThree is: " + Arrays.toString(bufferThree.array()));
System.out.println("BufferFour is: " + Arrays.toString(bufferFour.array()));
System.out.println("BufferFive is: " + Arrays.toString(bufferFive.array()) + "\n");
// Loop to compare buffers
for(int i = 0; i < 4; i++)
{
int comparison = 0;
String bufferToCompare = "";
// Compare buffers
if(i == 0)
{
comparison = bufferOne.compareTo(bufferTwo);
bufferToCompare = "BufferTwo";
}
else if(i == 1)
{
comparison = bufferOne.compareTo(bufferThree);
bufferToCompare = "BufferThree";
}
else if(i == 2)
{
comparison = bufferOne.compareTo(bufferFour);
bufferToCompare = "BufferFour";
}
else
{
comparison = bufferOne.compareTo(bufferFive);
bufferToCompare = "BufferFive";
}
// Output results
if(comparison < 0)
{
System.out.println("BufferOne is less than " + bufferToCompare);
}
else if(comparison == 0)
{
System.out.println("BufferOne is equal to " + bufferToCompare);
}
else
{
System.out.println("BufferOne is greater than " + bufferToCompare);
}
}
}
}

Explanation

First, multiple IntBuffer objects are initialized through the allocate() method. Each buffer has the capacity to store 33 elements, except bufferFive, which can hold 55 elements.

Next, a for-loop adds values to the buffers through the put() method. The rewind() method sets the current position of each buffer to the first index.

The code uses another for-loop in which the loop invariant determines which buffers are to be compared.

The objects bufferOne and bufferTwo are equal, so the compareTo() method in line 5757 returns 00.

On the other hand, bufferOne and bufferThree have a different sequence of elements, as the last element of both objects is different. Since the last element of bufferOne (66) is greater than the last element of bufferThree (44), the compareTo() method in line 6262 returns a positive number to signal that bufferOne is greater than bufferThree.

The second elements of bufferOne and bufferFour do not match. Since the second element of bufferOne (11) is less than the second element of bufferFour (33), the compareTo() method in line 6767 returns a negative number to signal that bufferOne is less than bufferFour.

In the final comparison, bufferOne and bufferFive have different capacities. Although the first 33 elements are identical in both buffers, bufferFive contains two more elements than bufferOne. Therefore, the compareTo() method in line 7272 picks bufferFive as lexicographically greater than bufferOne and returns a negative number.

Copyright ©2024 Educative, Inc. All rights reserved