The compareTo() method of java.nio.FloatBuffer class is used to compare one buffer to another. Two float 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 float elements are compared as if by invoking Float.compare(float, float), except that -0.0 and 0.0 are considered to be equal. Float.NaN is considered by this method to be equal to itself and greater than all other float values (including Float.POSITIVE_INFINITY). A float buffer is not comparable to any other type of object. Syntax :
public int compareTo(FloatBuffer that)
Parameter: This method takes a floatbuffer object as a parameter with which this buffer will be compared. Return Value: This method returns a negative integer, zero, or a positive integer as this buffer is less than, equal to, or greater than the given buffer. Below are the examples to illustrate the compareTo() method: Examples 1: When both FloatBuffer are equal.
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 fb int capacity1 = 3 ; // Creating the FloatBuffer try { // creating object of floatbuffer fb // and allocating size capacity FloatBuffer fb = FloatBuffer.allocate(capacity1); // putting the value in fb fb.put( 9 .56F); fb.put( 7 .61F); fb.put( 4 .61F); // rewind the float buffer fb.rewind(); // print the FloatBuffer System.out.println("FloatBuffer fb: " + Arrays.toString(fb.array())); // creating object of floatbuffer fb1 // and allocating size capacity FloatBuffer fb1 = FloatBuffer.allocate(capacity1); // putting the value in fb1 fb1.put( 9 .56F); fb1.put( 7 .61F); fb1.put( 4 .61F); // rewind the float buffer fb1.rewind(); // print the FloatBuffer System.out.println("FloatBuffer fb1: " + Arrays.toString(fb1.array())); // compare both buffer and store the value into integer int i = fb.compareTo(fb1); // if else condition if (i == 0 ) System.out.println("\nboth buffer are lexicographically equal"); else if (i >= 0 ) System.out.println("\nfb is lexicographically greater than fb1"); else System.out.println("\nfb is lexicographically less than fb1"); } catch (IllegalArgumentException e) { System.out.println("Exception throws : " + e); } catch (ReadOnlyBufferException e) { System.out.println("Exception throws : " + e); } } } |
FloatBuffer fb: [9.56, 7.61, 4.61] FloatBuffer fb1: [9.56, 7.61, 4.61] both buffer are lexicographically equal
Examples 2: When this FloatBuffer is greater than the passed FloatBuffer
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 fb int capacity1 = 3 ; // Creating the FloatBuffer try { // creating object of floatbuffer fb // and allocating size capacity FloatBuffer fb = FloatBuffer.allocate(capacity1); // putting the value in fb fb.put( 9 .56F); fb.put( 7 .61F); fb.put( 4 .61F); // rewind the float buffer fb.rewind(); // print the FloatBuffer System.out.println("FloatBuffer fb: " + Arrays.toString(fb.array())); // creating object of floatbuffer fb1 // and allocating size capacity FloatBuffer fb1 = FloatBuffer.allocate(capacity1); // putting the value in fb1 fb1.put( 8 .56F); fb1.put( 7 .61F); fb1.put( 4 .61F); // rewind the float buffer fb1.rewind(); // print the FloatBuffer System.out.println("FloatBuffer fb1: " + Arrays.toString(fb1.array())); // compare both buffer and store the value into integer int i = fb.compareTo(fb1); // if else condition if (i == 0 ) System.out.println("\nboth buffer are lexicographically equal"); else if (i >= 0 ) System.out.println("\nfb is lexicographically greater than fb1"); else System.out.println("\nfb is lexicographically less than fb1"); } catch (IllegalArgumentException e) { System.out.println("Exception throws : " + e); } catch (ReadOnlyBufferException e) { System.out.println("Exception throws : " + e); } } } |
FloatBuffer fb: [9.56, 7.61, 4.61] FloatBuffer fb1: [8.56, 7.61, 4.61] fb is lexicographically greater than fb1
Examples 3: When this FloatBuffer is less than the passed FloatBuffer
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 fb int capacity1 = 3 ; // Creating the FloatBuffer try { // creating object of floatbuffer fb // and allocating size capacity FloatBuffer fb = FloatBuffer.allocate(capacity1); // putting the value in fb fb.put( 8 .56F); fb.put( 7 .61F); fb.put( 4 .61F); // rewind the float buffer fb.rewind(); // print the FloatBuffer System.out.println("FloatBuffer fb: " + Arrays.toString(fb.array())); // creating object of floatbuffer fb1 // and allocating size capacity FloatBuffer fb1 = FloatBuffer.allocate(capacity1); // putting the value in fb1 fb1.put( 9 .56F); fb1.put( 7 .61F); fb1.put( 4 .61F); // rewind the float buffer fb1.rewind(); // print the FloatBuffer System.out.println("FloatBuffer fb1: " + Arrays.toString(fb1.array())); // compare both buffer and store the value into integer int i = fb.compareTo(fb1); // if else condition if (i == 0 ) System.out.println("\nboth buffer are lexicographically equal"); else if (i >= 0 ) System.out.println("\nfb is lexicographically greater than fb1"); else System.out.println("\nfb is lexicographically less than fb1"); } catch (IllegalArgumentException e) { System.out.println("Exception throws : " + e); } catch (ReadOnlyBufferException e) { System.out.println("Exception throws : " + e); } } } |
FloatBuffer fb: [8.56, 7.61, 4.61] FloatBuffer fb1: [9.56, 7.61, 4.61] fb is lexicographically less than fb1