Skip to content
Related Articles

Related Articles

ByteBuffer compareTo() method in Java With Examples
  • Last Updated : 20 Sep, 2018

The compareTo() method of java.nio.ByteBuffer class is used to compare one buffer to another.

Two byte 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 byte elements are compared as if by invoking Byte.compare(byte, byte).

A byte buffer is not comparable to any other type of object.

Syntax :

public int compareTo(ByteBuffer that)

Parameter: This method takes a ByteBuffer 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 are the examples to illustrate the compareTo() method:

Examples 1: When both ByteBuffer 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 bb
        int capacity1 = 3;
  
        // Creating the ByteBuffer
        try {
  
            // creating object of ByteBuffer bb
            // and allocating size capacity
            ByteBuffer bb = ByteBuffer.allocate(capacity1);
  
            // putting the byte to int typecast value in bb
            bb.put((byte)20);
            bb.put((byte)30);
            bb.put((byte)40);
  
            // rewind the  ByteBuffer
            bb.rewind();
  
            // print the  ByteBuffer
            System.out.println("ByteBuffer bb: "
                               + Arrays.toString(bb.array()));
  
            // creating object of  ByteBuffer bb1
            // and allocating size capacity
            ByteBuffer bb1 = ByteBuffer.allocate(capacity1);
  
            // putting the value in fb1
            bb1.put((byte)20);
            bb1.put((byte)30);
            bb1.put((byte)40);
  
            // revind the ByteBuffer
            bb1.rewind();
  
            // print the ByteBuffer
            System.out.println("ByteBuffer bb1: "
                               + Arrays.toString(bb1.array()));
  
            // compare both buffer and store the value into integer
            int i = bb.compareTo(bb1);
  
            // if else condition
            if (i == 0)
                System.out.println("\nboth buffer are lexicographically equal");
            else if (i >= 0)
                System.out.println("\nbb is lexicographically greater than bb1");
            else
                System.out.println("\nbb is lexicographically less than bb1");
        }
  
        catch (IllegalArgumentException e) {
            System.out.println("Exception throws : " + e);
        }
  
        catch (ReadOnlyBufferException e) {
            System.out.println("Exception throws : " + e);
        }
    }
}
Output:
ByteBuffer bb: [20, 30, 40]
ByteBuffer bb1: [20, 30, 40]

both buffer are lexicographically equal

Examples 2: When this ByteBuffer is greater than the passed ByteBuffer




// 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 bb
        int capacity1 = 3;
  
        // Creating the ByteBuffer
        try {
  
            // creating object of ByteBuffer bb
            // and allocating size capacity
            ByteBuffer bb = ByteBuffer.allocate(capacity1);
  
            // putting the byte to int typecast value in bb
            bb.put((byte)30);
            bb.put((byte)30);
            bb.put((byte)40);
  
            // rewind the  ByteBuffer
            bb.rewind();
  
            // print the  ByteBuffer
            System.out.println("ByteBuffer bb: "
                               + Arrays.toString(bb.array()));
  
            // creating object of  ByteBuffer bb1
            // and allocating size capacity
            ByteBuffer bb1 = ByteBuffer.allocate(capacity1);
  
            // putting the value in bb1
            bb1.put((byte)20);
            bb1.put((byte)30);
            bb1.put((byte)40);
  
            // revind the ByteBuffer
            bb1.rewind();
  
            // print the ByteBuffer
            System.out.println("ByteBuffer bb1: "
                               + Arrays.toString(bb1.array()));
  
            // compare both buffer and store the value into integer
            int i = bb.compareTo(bb1);
  
            // if else condition
            if (i == 0)
                System.out.println("\nboth buffer are lexicographically equal");
            else if (i >= 0)
                System.out.println("\nbb is lexicographically greater than bb1");
            else
                System.out.println("\nbb is lexicographically less than bb1");
        }
  
        catch (IllegalArgumentException e) {
            System.out.println("Exception throws : " + e);
        }
  
        catch (ReadOnlyBufferException e) {
            System.out.println("Exception throws : " + e);
        }
    }
}
Output:
ByteBuffer bb: [30, 30, 40]
ByteBuffer bb1: [20, 30, 40]

bb is lexicographically greater than bb1

Examples 3: When this ByteBuffer is less than the passed ByteBuffer




// 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 bb
        int capacity1 = 3;
  
        // Creating the ByteBuffer
        try {
  
            // creating object of ByteBuffer bb
            // and allocating size capacity
            ByteBuffer bb = ByteBuffer.allocate(capacity1);
  
            // putting the byte to int typecast value in bb
            bb.put((byte)20);
            bb.put((byte)30);
            bb.put((byte)40);
  
            // rewind the  ByteBuffer
            bb.rewind();
  
            // print the  ByteBuffer
            System.out.println("ByteBuffer bb: "
                               + Arrays.toString(bb.array()));
  
            // creating object of  ByteBuffer bb1
            // and allocating size capacity
            ByteBuffer bb1 = ByteBuffer.allocate(capacity1);
  
            // putting the value in fb1
            bb1.put((byte)40);
            bb1.put((byte)30);
            bb1.put((byte)40);
  
            // revind the ByteBuffer
            bb1.rewind();
  
            // print the ByteBuffer
            System.out.println("ByteBuffer bb1: "
                               + Arrays.toString(bb1.array()));
  
            // compare both buffer and store the value into integer
            int i = bb.compareTo(bb1);
  
            // if else condition
            if (i == 0)
                System.out.println("\nboth buffer are lexicographically equal");
            else if (i >= 0)
                System.out.println("\nbb is lexicographically greater than bb1");
            else
                System.out.println("\nbb is lexicographically less than bb1");
        }
  
        catch (IllegalArgumentException e) {
            System.out.println("Exception throws : " + e);
        }
  
        catch (ReadOnlyBufferException e) {
            System.out.println("Exception throws : " + e);
        }
    }
}
Output:
ByteBuffer bb: [20, 30, 40]
ByteBuffer bb1: [40, 30, 40]

bb is lexicographically less than bb1

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 :