El método equals() de java.nio.LongBuffer Class se usa para verificar si el búfer dado es igual o no a otro objeto.
Dos búferes largos 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 polongitudinalmente
iguales.
Este método considera que dos elementos Long a y b son iguales si (a == b) || (Largo.esNaN(a) && Largo.isNaN(b)). Los valores -0 y +0 se consideran iguales, a diferencia de Long.equals(Object).
Un búfer largo 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 LongBuffer 1 int capacity1 = 10; // Declaring the capacity of the LongBuffer 2 int capacity2 = 10; // Creating the LongBuffer try { // creating object of Longbuffer 1 // and allocating size capacity LongBuffer ib1 = LongBuffer.allocate(capacity1); // creating object of Longbuffer 2 // and allocating size capacity LongBuffer ib2 = LongBuffer.allocate(capacity2); // putting the value in Longbuffer 1 ib1.put(8); ib1.put(2, 9); ib1.rewind(); // putting the value in Longbuffer 2 ib2.put(8); ib2.put(2, 9); ib2.rewind(); // prLong the LongBuffer 1 System.out.println(" LongBuffer 1: " + Arrays.toString(ib1.array())); // prLong the LongBuffer 2 System.out.println(" LongBuffer 2: " + Arrays.toString(ib2.array())); // checking the equality of both LongBuffer 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"); } } }
LongBuffer 1: [8, 0, 9, 0, 0, 0, 0, 0, 0, 0] LongBuffer 2: [8, 0, 9, 0, 0, 0, 0, 0, 0, 0] 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 LongBuffer 1 int capacity1 = 10; // Declaring the capacity of the LongBuffer 2 int capacity2 = 5; // Creating the LongBuffer try { // creating object of Longbuffer 1 // and allocating size capacity LongBuffer ib1 = LongBuffer.allocate(capacity1); // creating object of Longbuffer 2 // and allocating size capacity LongBuffer ib2 = LongBuffer.allocate(capacity2); // putting the value in Longbuffer 1 ib1.put(8); ib1.put(2, 9); ib1.rewind(); // putting the value in Longbuffer 2 ib2.put(8); ib2.put(2, 9); ib2.rewind(); // prLong the LongBuffer 1 System.out.println(" LongBuffer 1: " + Arrays.toString(ib1.array())); // prLong the LongBuffer 2 System.out.println(" LongBuffer 2: " + Arrays.toString(ib2.array())); // checking the equality of both LongBuffer 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"); } } }
LongBuffer 1: [8, 0, 9, 0, 0, 0, 0, 0, 0, 0] LongBuffer 2: [8, 0, 9, 0, 0] 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 LongBuffer 1 int capacity1 = 10; // Declaring the capacity of the LongBuffer 2 int capacity2 = 10; // Creating the LongBuffer try { // creating object of Longbuffer 1 // and allocating size capacity LongBuffer ib1 = LongBuffer.allocate(capacity1); // creating object of Longbuffer 2 // and allocating size capacity LongBuffer ib2 = LongBuffer.allocate(capacity2); // putting the value in Longbuffer 1 ib1.put(8); ib1.put(2, 9); ib1.rewind(); // putting the value in Longbuffer 2 ib2.put(8); ib2.put(2, 9); ib2.put(3, 7); ib2.put(4, 4); ib2.rewind(); // prLong the LongBuffer 1 System.out.println(" LongBuffer 1: " + Arrays.toString(ib1.array())); // prLong the LongBuffer 2 System.out.println(" LongBuffer 2: " + Arrays.toString(ib2.array())); // checking the equality of both LongBuffer 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"); } } }
LongBuffer 1: [8, 0, 9, 0, 0, 0, 0, 0, 0, 0] LongBuffer 2: [8, 0, 9, 7, 4, 0, 0, 0, 0, 0] Both are not equal
Publicación traducida automáticamente
Artículo escrito por pawan_asipu y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA