Guava – Shorts Class

  • Post author:
  • Post category:Guava
  • Post comments:0 Comments
Shorts

Shorts is a utility class for primitive type short.

Class Declaration

Following is the declaration for com.google.common.primitives.Shorts class −

@GwtCompatible
public final class Shorts
   extends Object

Fields

Sr.NoField & Description
1static int BYTESThe number of bytes required to represent a primitive short value.
2static short MAX_POWER_OF_TWOThe largest power of two that can be represented as a short.

Methods

Sr.NoMethod & Description
1static List<Short> asList(short… backingArray)Returns a fixed-size list backed by the specified array, similar to Arrays.asList(Object[]).
2static short checkedCast(long value)Returns the short value that is equal to value, if possible.
3static int compare(short a, short b)Compares the two specified short values.
4static short[] concat(short[]… arrays)Returns the values from each provided array combined into a single array.
5static boolean contains(short[] array, short target)Returns true if target is present as an element anywhere in array.
6static short[] ensureCapacity(short[] array, int minLength, int padding)Returns an array containing the same values as array, but guaranteed to be of a specified minimum length.
7static short fromByteArray(byte[] bytes)Returns the short value whose big-endian representation is stored in the first 2 bytes of bytes; equivalent to ByteBuffer.wrap(bytes).getShort().
8static short fromBytes(byte b1, byte b2)Returns the short value whose byte representation is the given 2 bytes, in big-endian order; equivalent to Shorts.fromByteArray(new byte[] {b1, b2}).
9static int hashCode(short value)Returns a hash code for value; equal to the result of invoking ((Short) value).hashCode().
10static int indexOf(short[] array, short target)Returns the index of the first appearance of the value target in array.
11static int indexOf(short[] array, short[] target)Returns the start position of the first occurrence of the specified target within array, or -1 if there is no such occurrence.
12static String join(String separator, short… array)Returns a string containing the supplied short values separated by separator.
13static int lastIndexOf(short[] array, short target)Returns the index of the last appearance of the value target in array.
14static Comparator<short[]> lexicographicalComparator()Returns a comparator that compares two short arrays lexicographically.
15static short max(short… array)Returns the greatest value present in array.
16static short min(short… array)Returns the least value present in array.
17static short saturatedCast(long value)Returns the short nearest in value to value.
18static Converter<String,Short> stringConverter()Returns a serializable converter object that converts between strings and shorts using Short.decode(java.lang.String) and Short.toString().
19static short[] toArray(Collection<? extends Number> collection)Returns an array containing each value of collection, converted to a short value in the manner of Number.shortValue().
20static byte[] toByteArray(short value)Returns a big-endian representation of value in a 2-element byte array; equivalent to ByteBuffer.allocate(2).putShort(value).array().

Methods Inherited

This class inherits methods from the following class −

  • java.lang.Object

Example of Shorts Class

Create the following java program using any editor of your choice in say C:/> Guava.

GuavaTester.java

import java.util.List;

import com.google.common.primitives.Shorts;

public class GuavaTester {

   public static void main(String args[]) {
      GuavaTester tester = new GuavaTester();
      tester.testShorts();
   }

   private void testShorts() {
      short[] shortArray = {1,2,3,4,5,6,7,8,9};

      //convert array of primitives to array of objects
      List<Short> objectArray = Shorts.asList(shortArray);
      System.out.println(objectArray.toString());

      //convert array of objects to array of primitives
      shortArray = Shorts.toArray(objectArray);
      System.out.print("[ ");
      
      for(int i = 0; i< shortArray.length ; i++) {
         System.out.print(shortArray[i] + " ");
      }
      
      System.out.println("]");
      short data = 5;
      
      //check if element is present in the list of primitives or not
      System.out.println("5 is in list? " + Shorts.contains(shortArray, data));

      //Returns the minimum		
      System.out.println("Min: " + Shorts.min(shortArray));

      //Returns the maximum		
      System.out.println("Max: " + Shorts.max(shortArray));
      data = 2400;
      
      //get the byte array from an integer
      byte[] byteArray = Shorts.toByteArray(data);
      
      for(int i = 0; i< byteArray.length ; i++) {
         System.out.print(byteArray[i] + " ");
      }
   }
}

Verify the Result

Compile the class using javac compiler as follows −

C:\Guava>javac GuavaTester.java

Now run the GuavaTester to see the result.

C:\Guava>java GuavaTester

See the result.

[1, 2, 3, 4, 5, 6, 7, 8, 9]
[ 1 2 3 4 5 6 7 8 9 ]
5 is in list? true
Min: 1
Max: 9
9 96 

Previous Page:-Click Here

Leave a Reply