FloatBuffer compareTo() method in Java With Examples
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
import java.nio.*;
import java.util.*;
public class GFG {
public static void main(String[] args)
{
int capacity1 = 3 ;
try {
FloatBuffer fb = FloatBuffer.allocate(capacity1);
fb.put( 9 .56F);
fb.put( 7 .61F);
fb.put( 4 .61F);
fb.rewind();
System.out.println("FloatBuffer fb: "
+ Arrays.toString(fb.array()));
FloatBuffer fb1 = FloatBuffer.allocate(capacity1);
fb1.put( 9 .56F);
fb1.put( 7 .61F);
fb1.put( 4 .61F);
fb1.rewind();
System.out.println("FloatBuffer fb1: "
+ Arrays.toString(fb1.array()));
int i = fb.compareTo(fb1);
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);
}
}
}
|
Output:
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
import java.nio.*;
import java.util.*;
public class GFG {
public static void main(String[] args)
{
int capacity1 = 3 ;
try {
FloatBuffer fb = FloatBuffer.allocate(capacity1);
fb.put( 9 .56F);
fb.put( 7 .61F);
fb.put( 4 .61F);
fb.rewind();
System.out.println("FloatBuffer fb: "
+ Arrays.toString(fb.array()));
FloatBuffer fb1 = FloatBuffer.allocate(capacity1);
fb1.put( 8 .56F);
fb1.put( 7 .61F);
fb1.put( 4 .61F);
fb1.rewind();
System.out.println("FloatBuffer fb1: "
+ Arrays.toString(fb1.array()));
int i = fb.compareTo(fb1);
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);
}
}
}
|
Output:
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
import java.nio.*;
import java.util.*;
public class GFG {
public static void main(String[] args)
{
int capacity1 = 3 ;
try {
FloatBuffer fb = FloatBuffer.allocate(capacity1);
fb.put( 8 .56F);
fb.put( 7 .61F);
fb.put( 4 .61F);
fb.rewind();
System.out.println("FloatBuffer fb: "
+ Arrays.toString(fb.array()));
FloatBuffer fb1 = FloatBuffer.allocate(capacity1);
fb1.put( 9 .56F);
fb1.put( 7 .61F);
fb1.put( 4 .61F);
fb1.rewind();
System.out.println("FloatBuffer fb1: "
+ Arrays.toString(fb1.array()));
int i = fb.compareTo(fb1);
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);
}
}
}
|
Output:
FloatBuffer fb: [8.56, 7.61, 4.61]
FloatBuffer fb1: [9.56, 7.61, 4.61]
fb is lexicographically less than fb1
Last Updated :
06 Sep, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...