Tuesday, November 19, 2024
Google search engine
HomeLanguagesJavaDoubleBuffer compareTo() method in Java With Examples

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);
        }
    }
}


Output:

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);
        }
    }
}


Output:

DoubleBuffer db: [9.56, 7.61, 4.61]
DoubleBuffer db1: [8.56, 7.61, 4.61]

db is lexicographically greater than db1
RELATED ARTICLES

Most Popular

Recent Comments