IntBuffer equals() method in Java
Last Updated :
19 Sep, 2018
The
equals() method of
java.nio.IntBuffer Class is used to check whether or not the given buffer is equal to another object.
Two int 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.
This method considers two int elements a and b to be equal if (a == b) || (Int.isNaN(a) && Int.isNaN(b)). The values -0 and +0 are considered to be equal, unlike Int.equals(Object).
A int 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 IntBuffer 1
int capacity1 = 10;
// Declaring the capacity of the IntBuffer 2
int capacity2 = 10;
// Creating the IntBuffer
try {
// creating object of Intbuffer 1
// and allocating size capacity
IntBuffer ib1 = IntBuffer.allocate(capacity1);
// creating object of Intbuffer 2
// and allocating size capacity
IntBuffer ib2 = IntBuffer.allocate(capacity2);
// putting the value in Intbuffer 1
ib1.put(8);
ib1.put(2, 9);
ib1.rewind();
// putting the value in Intbuffer 2
ib2.put(8);
ib2.put(2, 9);
ib2.rewind();
// print the IntBuffer 1
System.out.println(" IntBuffer 1: "
+ Arrays.toString(ib1.array()));
// print the IntBuffer 2
System.out.println(" IntBuffer 2: "
+ Arrays.toString(ib2.array()));
// checking the equality of both IntBuffer
boolean ibb = ib1.equals(ib2);
// checking if else condition
if (ibb)
System.out.println("Both are equal");
else
System.out.println("Both are not equal");
}
catch (IllegalArgumentException e) {
System.out.println("IllegalArgumentException catched");
}
catch (ReadOnlyBufferException e) {
System.out.println("ReadOnlyBufferException catched");
}
}
}
Output:
IntBuffer 1: [8, 0, 9, 0, 0, 0, 0, 0, 0, 0]
IntBuffer 2: [8, 0, 9, 0, 0, 0, 0, 0, 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 IntBuffer 1
int capacity1 = 10;
// Declaring the capacity of the IntBuffer 2
int capacity2 = 5;
// Creating the IntBuffer
try {
// creating object of Intbuffer 1
// and allocating size capacity
IntBuffer ib1 = IntBuffer.allocate(capacity1);
// creating object of Intbuffer 2
// and allocating size capacity
IntBuffer ib2 = IntBuffer.allocate(capacity2);
// putting the value in Intbuffer 1
ib1.put(8);
ib1.put(2, 9);
ib1.rewind();
// putting the value in Intbuffer 2
ib2.put(8);
ib2.put(2, 9);
ib2.rewind();
// print the IntBuffer 1
System.out.println(" IntBuffer 1: "
+ Arrays.toString(ib1.array()));
// print the IntBuffer 2
System.out.println(" IntBuffer 2: "
+ Arrays.toString(ib2.array()));
// checking the equality of both IntBuffer
boolean ibb = ib1.equals(ib2);
// checking if else condition
if (ibb)
System.out.println("Both are equal");
else
System.out.println("Both are not equal");
}
catch (IllegalArgumentException e) {
System.out.println("IllegalArgumentException catched");
}
catch (ReadOnlyBufferException e) {
System.out.println("ReadOnlyBufferException catched");
}
}
}
Output:
IntBuffer 1: [8, 0, 9, 0, 0, 0, 0, 0, 0, 0]
IntBuffer 2: [8, 0, 9, 0, 0]
Both are not equal
Examples 3:
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 IntBuffer 1
int capacity1 = 10;
// Declaring the capacity of the IntBuffer 2
int capacity2 = 10;
// Creating the IntBuffer
try {
// creating object of Intbuffer 1
// and allocating size capacity
IntBuffer ib1 = IntBuffer.allocate(capacity1);
// creating object of Intbuffer 2
// and allocating size capacity
IntBuffer ib2 = IntBuffer.allocate(capacity2);
// putting the value in Intbuffer 1
ib1.put(8);
ib1.put(2, 9);
ib1.rewind();
// putting the value in Intbuffer 2
ib2.put(8);
ib2.put(2, 9);
ib2.put(3, 7);
ib2.put(4, 4);
ib2.rewind();
// print the IntBuffer 1
System.out.println(" IntBuffer 1: "
+ Arrays.toString(ib1.array()));
// print the IntBuffer 2
System.out.println(" IntBuffer 2: "
+ Arrays.toString(ib2.array()));
// checking the equality of both IntBuffer
boolean ibb = ib1.equals(ib2);
// checking if else condition
if (ibb)
System.out.println("Both are equal");
else
System.out.println("Both are not equal");
}
catch (IllegalArgumentException e) {
System.out.println("IllegalArgumentException catched");
}
catch (ReadOnlyBufferException e) {
System.out.println("ReadOnlyBufferException catched");
}
}
}
Output:
IntBuffer 1: [8, 0, 9, 0, 0, 0, 0, 0, 0, 0]
IntBuffer 2: [8, 0, 9, 7, 4, 0, 0, 0, 0, 0]
Both are not equal
Similar Reads
IntBuffer array() method in Java
The array() method of java.nio.IntBuffer Class is used to Return the int array that backs this buffer. Modifications to this buffer's content will cause the returned array's content to be modified, and vice versa. Invoke() the hasArray() method are used before invoking this method in order to ensure
3 min read
IntBuffer hasArray() method in Java
The hasArray() method of java.nio.IntBuffer class is used to ensure whether or not the given buffer is backed by an accessible int array. It returns true if there is an accessible backing array to this buffer, else it returns false. If this method returns true, then the array() and arrayOffset() met
2 min read
IntBuffer allocate() method in Java
The allocate() method of java.nio.IntBuffer Class is used to allocate a new int buffer next to the existing 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,
2 min read
IntBuffer compact() method in Java
The compact() method of java.nio.IntBuffer Class is used to compact the given buffer. The values between the buffer's current position and its limit are copied to the beginning of the buffer. The buffer's position is then set to n+1 and its limit is set to its capacity. The buffer's position is set
3 min read
IntBuffer arrayOffset() method in Java
The arrayOffset() method of java.nio.IntBuffer class is used to return the offset within the buffer's backing array of the first element of the buffer. It means that if this buffer is backed by an array, then buffer position p corresponds to array index p + arrayOffset(). Inorder to check whether th
3 min read
IntBuffer compareTo() method in Java
The compareTo() method of java.nio.IntBuffer class is used to compare one buffer to another. Two int 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 int eleme
4 min read
IntBuffer get() methods in Java | Set 1
get() The get() method of java.nio.IntBuffer Class is used to reads the int at the given buffer's current position, and then increments the position. Syntax : public abstract int get() Return Value: This method returns the int value at the buffer's current position. Throws: This method throws Buffer
6 min read
IntBuffer slice() method in Java
The slice() method of java.nio.IntBuffer Class is used to creates a new int buffer whose content is a shared subsequence of the given buffer's content. 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
3 min read
IntBuffer asReadOnlyBuffer() method in Java
The asReadOnlyBuffer() method of java.nio.IntBuffer Class is used to create a new, read-only int buffer with this buffer's content. The new buffer is a replica of this buffer. Hence changes made to this buffer's content will be visible in the new buffer. Since the new buffer is read-only, therefore
3 min read
IntBuffer flip() method in Java with Examples
The flip() method of java.nio.IntBuffer Class is used to flip this buffer. By flipping this buffer, it meant that the buffer will be trimmed to the current position and then the position will be changed to zero. During this process, if any mark is there on the buffer, then that mark will be automati
2 min read