El método compareTo() de la clase java.nio.ByteBuffer se utiliza para comparar un búfer con otro.
Los búferes de dos bytes se comparan comparando sus secuencias de elementos restantes lexicográficamente, sin tener en cuenta la posición inicial de cada secuencia dentro de su búfer correspondiente. Los pares de elementos de byte se comparan como si se invocara Byte.compare(byte, byte).
Un búfer de bytes no es comparable a ningún otro tipo de objeto.
Sintaxis:
public int compareTo(ByteBuffer that)
Parámetro: este método toma un objeto ByteBuffer como parámetro con el que se comparará este búfer.
Valor de retorno: este método devuelve un número entero negativo, cero o un número entero positivo, ya que este búfer es menor, igual o mayor que el búfer dado.
A continuación se muestran los ejemplos para ilustrar el método compareTo() :
Ejemplos 1: cuando ambos ByteBuffer son iguales.
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); } } }
ByteBuffer bb: [20, 30, 40] ByteBuffer bb1: [20, 30, 40] both buffer are lexicographically equal
Ejemplos 2: cuando este ByteBuffer es mayor que el ByteBuffer pasado
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); } } }
ByteBuffer bb: [30, 30, 40] ByteBuffer bb1: [20, 30, 40] bb is lexicographically greater than bb1
Ejemplos 3: cuando este ByteBuffer es menor que el ByteBuffer pasado
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); } } }
ByteBuffer bb: [20, 30, 40] ByteBuffer bb1: [40, 30, 40] bb is lexicographically less than bb1
Publicación traducida automáticamente
Artículo escrito por RohitPrasad3 y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA