ByteBuffer equals() method in Java with Examples
Last Updated :
22 Oct, 2019
The
equals() method of
java.nio.ByteBuffer class is used to check whether or not the given buffer is equal to another object.
Two byte 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, considered independently of their starting positions, are pointwise equal.
A byte buffer is not equal to any other type of object.
Syntax:
public boolean equals(Object ob)
Parameters: This method takes the ob(The object to which this buffer is to be compared) as a parameter.
Return Value: This method returns true if, and only if, this buffer is equal to the given object.
Below are the examples to illustrate the
equals() method:
Examples 1:
Java
// Java program to demonstrate
// equals() method
import java.nio.*;
import java.util.*;
public class GFG {
public static void main(String[] args)
{
// Declaring the capacity of the ByteBuffer 1
int capacity1 = 5;
// Declaring the capacity of the ByteBuffer 2
int capacity2 = 5;
// Creating the ByteBuffer
try {
// creating object of ByteBuffer 1
// and allocating size capacity
ByteBuffer bb1 = ByteBuffer.allocate(capacity1);
// creating object of ByteBuffer 2
// and allocating size capacity
ByteBuffer bb2 = ByteBuffer.allocate(capacity2);
// putting the int to byte typecast value in ByteBuffer 1
bb1.put((byte)20);
bb1.put((byte)30);
bb1.put((byte)40);
bb1.rewind();
// putting the value in ByteBuffer 2
bb2.put((byte)20);
bb2.put((byte)30);
bb2.put((byte)40);
bb2.rewind();
// print the ByteBuffer 1
System.out.println(" ByteBuffer 1: "
+ Arrays.toString(bb1.array()));
// print the ByteBuffer 2
System.out.println(" ByteBuffer 2: "
+ Arrays.toString(bb2.array()));
// checking the equality of both ByteBuffer
boolean b = bb1.equals(bb2);
// checking if else condition
if (b)
System.out.println(" both are equal");
else
System.out.println(" both are not equal");
}
catch (IllegalArgumentException e) {
System.out.println("Exception thrown : " + e);
}
catch (ReadOnlyBufferException e) {
System.out.println("Exception thrown : " + e);
}
}
}
Output:
ByteBuffer 1: [20, 30, 40, 0, 0]
ByteBuffer 2: [20, 30, 40, 0, 0]
both are equal
Examples 2:
Java
// Java program to demonstrate
// equals() method
import java.nio.*;
import java.util.*;
public class GFG {
public static void main(String[] args)
{
// Declaring the capacity of the ByteBuffer 1
int capacity1 = 5;
// Declaring the capacity of the ByteBuffer 2
int capacity2 = 3;
// Creating the ByteBuffer
try {
// creating object of ByteBuffer 1
// and allocating size capacity
ByteBuffer bb1 = ByteBuffer.allocate(capacity1);
// creating object of ByteBuffer 2
// and allocating size capacity
ByteBuffer bb2 = ByteBuffer.allocate(capacity2);
// putting the int to byte typecast value in ByteBuffer 1
bb1.put((byte)20);
bb1.put((byte)30);
bb1.put((byte)40);
bb1.rewind();
// putting the value in ByteBuffer 2
bb2.put((byte)20);
bb2.put((byte)30);
bb2.put((byte)40);
bb2.rewind();
// print the ByteBuffer 1
System.out.println(" ByteBuffer 1: "
+ Arrays.toString(bb1.array()));
// print the ByteBuffer 2
System.out.println(" ByteBuffer 2: "
+ Arrays.toString(bb2.array()));
// checking the equality of both ByteBuffer
boolean b = bb1.equals(bb2);
// checking if else condition
if (b)
System.out.println(" both are equal");
else
System.out.println(" both are not equal");
}
catch (IllegalArgumentException e) {
System.out.println("Exception thrown : " + e);
}
catch (ReadOnlyBufferException e) {
System.out.println("Exception thrown : " + e);
}
}
}
Output:
ByteBuffer 1: [20, 30, 40, 0, 0]
ByteBuffer 2: [20, 30, 40]
both are not equal
Similar Reads
DoubleBuffer equals() method in Java with Examples The equals() method of java.nio.DoubleBuffer Class is used to check whether or not the given buffer is equal to another object. Two double 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,
3 min read
Byte equals() method in Java with examples The equals() method of Byte class is a built in method in Java which is used to compare the equality given Object with the instance of Byte invoking the equals() method. Syntax ByteObject.equals(Object a) Parameters: It takes an Object type object a as input which is to be compared with the instance
2 min read
BitSet equals() Method in Java with Examples The equals() method of Java BitSet class is used to check for equality between two bitsets. It verifies whether the elements of one set passed as a parameter is equal to the elements of this set or not. The method returns true if the bitsets match else false. Syntax: Bit_Set1.equals(Bit_Set2) Parame
2 min read
ByteBuffer duplicate() method in Java with Examples The duplicate() method of java.nio.ByteBuffer class is used to create a new 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, and vice versa; the two buffers' position, limit,
3 min read
ByteBuffer get() method in Java with Examples get() The get() method of java.nio.ByteBuffer class is used to read the byte at the buffer's current position, and then increments the position. Syntax : public abstract byte get() Return Value: This method returns the byte at the buffer's current position. Throws: This method throws BufferUnderflow
6 min read
ByteBuffer compareTo() method in Java With Examples 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 el
4 min read