El método equals() de java.nio.CharBuffer Class se usa para verificar si el búfer dado es igual o no a otro objeto.
Dos búferes de caracteres son iguales si, y solo si,
- Tienen el mismo tipo de elemento,
- Tienen el mismo número de elementos restantes, y
- Las dos secuencias de elementos restantes, consideradas independientemente de sus posiciones iniciales, son puntos
iguales.
Este método considera que dos elementos char ‘a’ y ‘b’ son iguales si (‘a’ == ‘b’) || (Char.isNaN(a) && Char.isNaN(b)). Los valores -0 y +0 se consideran iguales, a diferencia de Char.equals(Object).
Un búfer de caracteres no es igual a ningún otro tipo de objeto.
Sintaxis:
public boolean equals(Object ob)
Parámetros: este método toma el ob (el objeto con el que se va a comparar este búfer) como parámetro.
Valor devuelto: este método devuelve verdadero si, y solo si, este búfer es igual al objeto dado.
A continuación se muestran los ejemplos para ilustrar el método equals() :
Ejemplos 1:
// 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 CharBuffer 1 int capacity1 = 10; // Declaring the capacity of the CharBuffer 2 int capacity2 = 10; // Creating the CharBuffer try { // creating object of Charbuffer 1 // and allocating size capacity CharBuffer cb1 = CharBuffer.allocate(capacity1); // creating object of Charbuffer 2 // and allocating size capacity CharBuffer cb2 = CharBuffer.allocate(capacity2); // putting the value in Charbuffer 1 cb1.put('a'); cb1.put(2, 'b'); cb1.rewind(); // putting the value in Charbuffer 2 cb2.put('a'); cb2.put(2, 'b'); cb2.rewind(); // print the CharBuffer 1 System.out.println(" CharBuffer 1: " + Arrays.toString(cb1.array())); // print the CharBuffer 2 System.out.println(" CharBuffer 2: " + Arrays.toString(cb2.array())); // checking the equality of both CharBuffer boolean cbb = cb1.equals(cb2); // checking if else condition if (cbb) 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"); } } }
Producción:
CharBuffer 1: [a,, b,,,,,,, ] CharBuffer 2: [a,, b,,,,,,, ] Both are equal
Ejemplos 2:
// 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 CharBuffer 1 int capacity1 = 10; // Declaring the capacity of the CharBuffer 2 int capacity2 = 5; // Creating the CharBuffer try { // creating object of Charbuffer 1 // and allocating size capacity CharBuffer cb1 = CharBuffer.allocate(capacity1); // creating object of Charbuffer 2 // and allocating size capacity CharBuffer cb2 = CharBuffer.allocate(capacity2); // putting the value in Charbuffer 1 cb1.put('a'); cb1.put(2, 'b'); cb1.rewind(); // putting the value in Charbuffer 2 cb2.put('a'); cb2.put(2, 'b'); cb2.rewind(); // print the CharBuffer 1 System.out.println(" CharBuffer 1: " + Arrays.toString(cb1.array())); // print the CharBuffer 2 System.out.println(" CharBuffer 2: " + Arrays.toString(cb2.array())); // checking the equality of both CharBuffer boolean cbb = cb1.equals(cb2); // checking if else condition if (cbb) 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"); } } }
Producción:
CharBuffer 1: [a,, b,,,,,,, ] CharBuffer 2: [a,, b,, ] Both are not equal
Ejemplos 3:
// 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 CharBuffer 1 int capacity1 = 10; // Declaring the capacity of the CharBuffer 2 int capacity2 = 10; // Creating the CharBuffer try { // creating object of Charbuffer 1 // and allocating size capacity CharBuffer cb1 = CharBuffer.allocate(capacity1); // creating object of Charbuffer 2 // and allocating size capacity CharBuffer cb2 = CharBuffer.allocate(capacity2); // putting the value in Charbuffer 1 cb1.put('a'); cb1.put(2, 'b'); cb1.rewind(); // putting the value in Charbuffer 2 cb2.put('a'); cb2.put(2, 'b'); cb2.put(3, 'c'); cb2.put(4, 'd'); cb2.rewind(); // print the CharBuffer 1 System.out.println(" CharBuffer 1: " + Arrays.toString(cb1.array())); // print the CharBuffer 2 System.out.println(" CharBuffer 2: " + Arrays.toString(cb2.array())); // checking the equality of both CharBuffer boolean cbb = cb1.equals(cb2); // checking if else condition if (cbb) 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"); } } }
Producción:
CharBuffer 1: [a,, b,,,,,,, ] CharBuffer 2: [a,, b, c, d,,,,, ] Both are not equal