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 and 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 anotherIntBuffer
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)
The compareTo()
method takes a single mandatory parameter: the object to compare with the buffer.
The compareTo()
method compares two IntBuffer
objects and returns one of the following:
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.IntBuffer
objects are equal, then the compareTo()
method returns .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.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 instanceIntBuffer 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 buffersfor(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 0bufferOne.rewind();bufferTwo.rewind();bufferThree.rewind();bufferFour.rewind();bufferFive.rewind();// Print buffersSystem.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 buffersfor(int i = 0; i < 4; i++){int comparison = 0;String bufferToCompare = "";// Compare buffersif(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 resultsif(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);}}}}
First, multiple IntBuffer
objects are initialized through the allocate()
method. Each buffer has the capacity to store elements, except bufferFive
, which can hold 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 returns .
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
() is greater than the last element of bufferThree
(), the compareTo()
method in line 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
() is less than the second element of bufferFour
(), the compareTo()
method in line 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 elements are identical in both buffers, bufferFive
contains two more elements than bufferOne
. Therefore, the compareTo()
method in line picks bufferFive
as lexicographically greater than bufferOne
and returns a negative number.