El método compareTo() de la clase java.nio.charBuffer se usa para comparar un búfer con otro. Dos búferes de caracteres se comparan comparando sus secuencias de elementos restantes lexicográficamente, sin considerar la posición inicial de cada secuencia dentro de su búfer correspondiente. Los pares de elementos char se comparan como si se invocara Char.compare(char, char). cualquier otro tipo de objeto no se puede comparar con char buffer.
Sintaxis:
public int compareTo(CharBuffer that)
Parámetro: este método toma un objeto charbuffer 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():
Ejemplo 1: Cuando ambos CharBuffer son iguales.
// 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 cb int capacity1 = 3; // Creating the CharBuffer try { // creating object of charbuffer cb // and allocating size capacity CharBuffer cb = CharBuffer.allocate(capacity1); // putting the value in cb cb.put('a'); cb.put('b'); cb.put('c'); // revind the float buffer cb.rewind(); // print the Charbuffer System.out.println("Charbuffer cb: " + Arrays.toString(cb.array())); // creating object of floatbuffer cb1 // and allocating size capacity CharBuffer cb1 = CharBuffer.allocate(capacity1); // putting the value in cb1 cb1.put('a'); cb1.put('b'); cb1.put('c'); // revind the float buffer cb1.rewind(); // print the Charbuffer System.out.println("Charbuffer cb1: " + Arrays.toString(cb1.array())); // compare both buffer and store the value into integer int i = cb.compareTo(cb1); // if else condition if (i == 0) System.out.println("\nboth buffer are" + "lexicographically equal"); else if (i >= 0) System.out.println("\ncb is lexicographically" + "greater than cb1"); else System.out.println("\ncb is lexicographically" + "less than cb1"); } catch (IllegalArgumentException e) { System.out.println("Exception throws : " + e); } catch (ReadOnlyBufferException e) { System.out.println("Exception throws : " + e); } } }
Charbuffer cb: [a, b, c] Charbuffer cb1: [a, b, c] both buffer arelexicographically equal
Ejemplo 2: cuando este FloatBuffer es mayor que el FloatBuffer pasado
// 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 cb int capacity1 = 3; // Creating the CharBuffer try { // creating object of floatbuffer cb // and allocating size capacity CharBuffer cb = CharBuffer.allocate(capacity1); // putting the value in cb cb.put('g'); cb.put('b'); cb.put('c'); // revind the float buffer cb.rewind(); // print the CharBuffer System.out.println("CharBuffer cb: " + Arrays.toString(cb.array())); // creating object of floatbuffer cb1 // and allocating size capacity CharBuffer cb1 = CharBuffer.allocate(capacity1); // putting the value in cb1 cb1.put('a'); cb1.put('b'); cb1.put('c'); // revind the float buffer cb1.rewind(); // print the CharBuffer System.out.println("CharBuffer cb1: " + Arrays.toString(cb1.array())); // compare both buffer and store // the value into integer int i = cb.compareTo(cb1); // if else condition if (i == 0) System.out.println("\nboth buffer are" + " lexicographically equal"); else if (i >= 0) System.out.println("\ncb is lexicographically" + " greater than cb1"); else System.out.println("\ncb is lexicographically" + " less than cb1"); } catch (IllegalArgumentException e) { System.out.println("Exception throws : " + e); } catch (ReadOnlyBufferException e) { System.out.println("Exception throws : " + e); } } }
CharBuffer cb: [g, b, c] CharBuffer cb1: [a, b, c] cb is lexicographically greater than cb1
Ejemplo 3: cuando este FloatBuffer es menor que el FloatBuffer pasado
// 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 cb int capacity1 = 3; // Creating the CharBuffer try { // creating object of CharBuffer cb // and allocating size capacity CharBuffer cb = CharBuffer.allocate(capacity1); // putting the value in cb cb.put('a'); cb.put('b'); cb.put('c'); // revind the float buffer cb.rewind(); // print the CharBuffer System.out.println("CharBuffer cb: " + Arrays.toString(cb.array())); // creating object of CharBuffer cb1 // and allocating size capacity CharBuffer cb1 = CharBuffer.allocate(capacity1); // putting the value in cb1 cb1.put('g'); cb1.put('b'); cb1.put('c'); // revind the float buffer cb1.rewind(); // print the CharBuffer System.out.println("CharBuffer cb1: " + Arrays.toString(cb1.array())); // compare both buffer and store the value into integer int i = cb.compareTo(cb1); // if else condition if (i == 0) System.out.println("\nboth buffer are" + "lexicographically equal"); else if (i >= 0) System.out.println("\ncb is lexicographically" + "greater than cb1"); else System.out.println("\ncb is lexicographically" + "less than cb1"); } catch (IllegalArgumentException e) { System.out.println("Exception throws : " + e); } catch (ReadOnlyBufferException e) { System.out.println("Exception throws : " + e); } } }
CharBuffer cb: [a, b, c] CharBuffer cb1: [g, b, c] cb is lexicographicallyless than cb1