Open In App

ByteBuffer compareTo() method in Java With Examples

Last Updated : 06 Jun, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

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




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




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




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

 



Similar Reads

ByteBuffer allocate() method in Java with Examples
The allocate() method of java.nio.ByteBuffer class is used to allocate a new byte buffer. The new buffer's position will be zero, its limit will be its capacity, its mark will be undefined, and each of its elements will be initialized to zero. It will have a backing array, and its array offset will be zero. Syntax : public static ByteBuffer allocat
2 min read
ByteBuffer asCharBuffer() method in Java with Examples
The asCharBuffer() method of java.nio.ByteBuffer class is used to create a view of this byte buffer as a char buffer.The content of the new buffer will start at this buffer's current position. Changes to this buffer's content will be visible in the new buffer, and vice versa; the two buffers' position, limit, and mark values will be independent.The
3 min read
ByteBuffer array() method in Java with Examples
The array() method of java.nio.ByteBuffer class is used to return the byte array that backs the taken buffer.Modifications to this buffer's content will cause the returned array's content to be modified, and vice versa.Invoke the hasArray() method before invoking this method in order to ensure that this buffer has an accessible backing array. Synta
3 min read
ByteBuffer asFloatBuffer() method in Java with Examples
The asFloatBuffer() method of java.nio.ByteBuffer class is used to create view of this byte buffer as a float buffer.The content of the new buffer will start from this buffer's current position. Changes made to this buffer's content will be visible in the new buffer, and vice versa; the two buffers' position, limit, and mark values will be independ
3 min read
ByteBuffer asDoubleBuffer() method in Java with Examples
The asDoubleBuffer() method of java.nio.ByteBuffer class is used to create a view of this byte buffer as a double buffer.The content of the new buffer will start from this buffer's current position. Changes made to this buffer's content will be visible in the new buffer, and vice versa; the two buffers' position, limit, and mark values will be inde
3 min read
ByteBuffer asIntBuffer() method in Java with Examples
The asIntBuffer() method of java.nio.ByteBuffer class is used to create a view of this byte buffer as an int buffer. The content of the new buffer will start from this buffer's current position. Changes made to this buffer's content will be visible in the new buffer, and vice versa; the two buffers' position, limit, and mark values will be independ
3 min read
ByteBuffer arrayOffset() method in Java with Examples
The arrayOffset() method of java.nio.ByteBuffer class is used to return the offset within the given buffer's backing array of the first element of the buffer. If this buffer is backed by an array then buffer position p corresponds to array index p + arrayOffset(). Invoke the hasArray method before invoking this method in order to ensure that this b
2 min read
ByteBuffer asLongBuffer() method in Java with Examples
The asLongBuffer() method of java.nio.ByteBuffer class is used to create a view of this byte buffer as a long buffer.The content of the new buffer will start at this buffer's current position. Changes to this buffer's content will be visible in the new buffer, and vice versa; the two buffers' position, limit, and mark values will be independent.The
3 min read
ByteBuffer asShortBuffer() method in Java with Examples
The asShortBuffer() method of java.nio.ByteBuffer class is used to create a view of this byte buffer as a short buffer.The content of the new buffer will start at this buffer's current position. Changes to this buffer's content will be visible in the new buffer, and vice versa; the two buffers' position, limit, and mark values will be independent.T
3 min read
ByteBuffer asReadOnlyBuffer() method in Java with Examples
The asReadOnlyBuffer() method of java.nio.ByteBuffer class is used to create a new, read-only byte buffer that shares this buffer's content. The content of the new buffer will be that of this buffer. Changes to this buffer's content will be visible in the new buffer; the new buffer itself, however, will be read-only and will not allow the shared co
3 min read
Practice Tags :