DoubleBuffer compareTo() method in Java With Examples
The compareTo() method of java.nio.DoubleBuffer class is used to compare one buffer to another. Two double 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 Double.compare(double, double), except that -0.0 and 0.0 are considered to be equal. Double.NaN is considered by this method to be equal to itself and greater than all other double values (including Double.POSITIVE_INFINITY). A double buffer is not comparable to any other type of object.
Syntax:
public int compareTo(DoubleBuffer that)
Parameter: This method takes a doublebuffer 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 DoubleBuffer are equal.
// 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 db int capacity1 = 3; // Creating the DoubleBuffer try { // creating object of Doublebuffer db // and allocating size capacity DoubleBuffer db = DoubleBuffer.allocate(capacity1); // putting the value in db db.put(9.56); db.put(7.61); db.put(4.61); // revind the Double buffer db.rewind(); // print the DoubleBuffer System.out.println("DoubleBuffer db: " + Arrays.toString(db.array())); // creating object of Doublebuffer db1 // and allocating size capacity DoubleBuffer db1 = DoubleBuffer.allocate(capacity1); // putting the value in db1 db1.put(9.56); db1.put(7.61); db1.put(4.61); // revind the Double buffer db1.rewind(); // print the DoubleBuffer System.out.println("DoubleBuffer db1: " + Arrays.toString(db1.array())); // compare both buffer and store the value into integer int i = db.compareTo(db1); // if else condition if (i == 0) System.out.println("\nboth buffer are lexicographically equal"); else if (i >= 0) System.out.println("\ndb is lexicographically greater than db1"); else System.out.println("\ndb is lexicographically less than db1"); } catch (IllegalArgumentException e) { System.out.println("Exception throws : " + e); } catch (ReadOnlyBufferException e) { System.out.println("Exception throws : " + e); } } } |
DoubleBuffer db: [9.56, 7.61, 4.61] DoubleBuffer db1: [9.56, 7.61, 4.61] both buffer are lexicographically equal
Examples 2: When this DoubleBuffer is greater than the passed DoubleBuffer
// 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 db int capacity1 = 3; // Creating the DoubleBuffer try { // creating object of Doublebuffer db // and allocating size capacity DoubleBuffer db = DoubleBuffer.allocate(capacity1); // putting the value in db db.put(9.56); db.put(7.61); db.put(4.61); // revind the Double buffer db.rewind(); // print the DoubleBuffer System.out.println("DoubleBuffer db: " + Arrays.toString(db.array())); // creating object of Doublebuffer db1 // and allocating size capacity DoubleBuffer db1 = DoubleBuffer.allocate(capacity1); // putting the value in db1 db1.put(8.56); db1.put(7.61); db1.put(4.61); // revind the Double buffer db1.rewind(); // print the DoubleBuffer System.out.println("DoubleBuffer db1: " + Arrays.toString(db1.array())); // compare both buffer and store the value into integer int i = db.compareTo(db1); // if else condition if (i == 0) System.out.println("\nboth buffer are lexicographically equal"); else if (i >= 0) System.out.println("\ndb is lexicographically greater than db1"); else System.out.println("\ndb is lexicographically less than db1"); } catch (IllegalArgumentException e) { System.out.println("Exception throws : " + e); } catch (ReadOnlyBufferException e) { System.out.println("Exception throws : " + e); } } } |
DoubleBuffer db: [9.56, 7.61, 4.61] DoubleBuffer db1: [8.56, 7.61, 4.61] db is lexicographically greater than db1
Recommended Posts:
- DoubleBuffer wrap() method in Java with Examples
- DoubleBuffer allocate() method in Java With Examples
- DoubleBuffer compact() method in Java With Examples
- DoubleBuffer arrayOffset() method in Java With Examples
- DoubleBuffer slice() method in Java with Examples
- DoubleBuffer equals() method in Java with Examples
- DoubleBuffer array() method in Java With Examples
- DoubleBuffer duplicate() method in Java with Examples
- DoubleBuffer asReadOnlyBuffer() method in Java with Examples
- DoubleBuffer hasArray() method in Java with Examples
- ChronoZonedDateTime compareTo() method in Java with Examples
- FloatBuffer compareTo() method in Java With Examples
- Date compareTo() method in Java with examples
- Short compareTo() method in Java with Examples
- LocalDateTime compareTo() method in Java with Examples
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



