ShortBuffer compareTo method in Java with Examples

The compareTo() method of java.nio.ShortBuffer class is used to compare one buffer to another.

Two short 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 short elements are compared as if by invoking Short.compare(short, short). A short buffer is not comparable to any other type of object.

Syntax:

public int compareTo(ShortBuffer that)

Parameter: This method takes a shortbuffer 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:

Program 1: When both FloatBuffer are equal.

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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 sb
        int capacity1 = 3;
  
        // Creating the ShortBuffer
        try {
  
            // creating object of shortbuffer sb
            // and allocating size capacity
            ShortBuffer sb = ShortBuffer.allocate(capacity1);
  
            // putting the value in sb
            sb.put((short)956);
            sb.put((short)761);
            sb.put((short)461);
  
            // revind the short buffer
            sb.rewind();
  
            // print the ShortBuffer
            System.out.println(" ShortBuffer sb: "
                               + Arrays.toString(sb.array()));
  
            // creating object of shortbuffer sb1
            // and allocating size capacity
            ShortBuffer sb1 = ShortBuffer.allocate(capacity1);
  
            // putting the value in sb1
            sb1.put((short)956);
            sb1.put((short)761);
            sb1.put((short)461);
  
            // revind the short buffer
            sb1.rewind();
  
            // print the shortBuffer
            System.out.println(" ShortBuffer sb1: "
                               + Arrays.toString(sb1.array()));
  
            // compare both buffer and store the value into integer
            int i = sb.compareTo(sb1);
  
            // 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);
        }
    }
}

chevron_right


Output:

ShortBuffer sb: [956, 761, 461]
ShortBuffer sb1: [956, 761, 461]

both buffer are lexicographically equal

Program 2: When this ShortBuffer is greater than the passed ShortBuffer

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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 sb
        int capacity1 = 3;
  
        // Creating the ShortBuffer
        try {
  
            // creating object of shortbuffer sb
            // and allocating size capacity
            ShortBuffer sb = ShortBuffer.allocate(capacity1);
  
            // putting the value in sb
            sb.put((short)956);
            sb.put((short)761);
            sb.put((short)41);
  
            // revind the short buffer
            sb.rewind();
  
            // print the ShortBuffer
            System.out.println(" ShortBuffer s: "
                               + Arrays.toString(sb.array()));
  
            // creating object of shortbuffer sb1
            // and allocating size capacity
            ShortBuffer sb1 = ShortBuffer.allocate(capacity1);
  
            // putting the value in sb1
            sb1.put((short)856);
            sb1.put((short)761);
            sb1.put((short)461);
  
            // revind the short buffer
            sb1.rewind();
  
            // print the ShortBuffer
            System.out.println(" ShortBuffer sb1: "
                               + Arrays.toString(sb1.array()));
  
            // compare both buffer and store the value into integer
            int i = sb.compareTo(sb1);
  
            // 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);
        }
    }
}

chevron_right


Output:

ShortBuffer s: [956, 761, 41]
ShortBuffer sb1: [856, 761, 461]

fb is lexicographically greater than fb1

Program 3: When this ShortBuffer is less than the passed ShortBuffer

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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 sb
        int capacity1 = 3;
  
        // Creating the ShortBuffer
        try {
  
            // creating object of shortbuffer sb
            // and allocating size capacity
            ShortBuffer sb = ShortBuffer.allocate(capacity1);
  
            // putting the value in sb
            sb.put((short)856);
            sb.put((short)761);
            sb.put((short)461);
  
            // revind the short buffer
            sb.rewind();
  
            // print the ShortBuffer
            System.out.println(" ShortBuffer sb: "
                               + Arrays.toString(sb.array()));
  
            // creating object of shortbuffer sb1
            // and allocating size capacity
            ShortBuffer sb1 = ShortBuffer.allocate(capacity1);
  
            // putting the value in sb1
            sb1.put((short)956);
            sb1.put((short)761);
            sb1.put((short)461);
  
            // revind the short buffer
            sb1.rewind();
  
            // print the ShortBuffer
            System.out.println(" ShortBuffer sb1: "
                               + Arrays.toString(sb1.array()));
  
            // compare both buffer and store the value into integer
            int i = sb.compareTo(sb1);
  
            // 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);
        }
    }
}

chevron_right


Output:

ShortBuffer sb: [856, 761, 461]
ShortBuffer sb1: [956, 761, 461]

fb is lexicographically less than fb1


My Personal Notes arrow_drop_up

Budding Web DeveloperKeen learnerAverage CoderDancer&Social Activist

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.