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.
import java.nio.*;
import java.util.*;
public class GFG {
public static void main(String[] args)
{
int capacity1 = 3 ;
try {
DoubleBuffer db = DoubleBuffer.allocate(capacity1);
db.put( 9.56 );
db.put( 7.61 );
db.put( 4.61 );
db.rewind();
System.out.println( "DoubleBuffer db: "
+ Arrays.toString(db.array()));
DoubleBuffer db1 = DoubleBuffer.allocate(capacity1);
db1.put( 9.56 );
db1.put( 7.61 );
db1.put( 4.61 );
db1.rewind();
System.out.println( "DoubleBuffer db1: "
+ Arrays.toString(db1.array()));
int i = db.compareTo(db1);
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
import java.nio.*;
import java.util.*;
public class GFG {
public static void main(String[] args)
{
int capacity1 = 3 ;
try {
DoubleBuffer db = DoubleBuffer.allocate(capacity1);
db.put( 9.56 );
db.put( 7.61 );
db.put( 4.61 );
db.rewind();
System.out.println( "DoubleBuffer db: "
+ Arrays.toString(db.array()));
DoubleBuffer db1 = DoubleBuffer.allocate(capacity1);
db1.put( 8.56 );
db1.put( 7.61 );
db1.put( 4.61 );
db1.rewind();
System.out.println( "DoubleBuffer db1: "
+ Arrays.toString(db1.array()));
int i = db.compareTo(db1);
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
Last Updated :
04 Dec, 2018
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...