Shorts is a utility class for primitive type short. It provides Static utility methods pertaining to short primitives, that are not already found in either Short or Arrays.
Declaration :
@GwtCompatible(emulated=true)
public final class Shorts
extends Object
Below table shows the Field summary for Guava Shorts Class :

Some of the methods provided by Guava Shorts Class are :

Exceptions :
- checkedCast : IllegalArgumentException if value is greater than Short.MAX_VALUE or less than Short.MIN_VALUE
- min : IllegalArgumentException if array is empty.
- max : IllegalArgumentException if array is empty.
- fromByteArray : IllegalArgumentException if bytes has fewer than 2 elements.
- ensureCapacity : IllegalArgumentException if minLength or padding is negative.
- toArray : NullPointerException if collection or any of its elements is null.
Below table shows some other methods provided by Guava Shorts Class :

Below given are some examples showing the implementation of Guava Shorts Class methods :
Example 1 :
import com.google.common.primitives.Shorts;
import java.util.*;
class GFG {
public static void main(String[] args)
{
short arr[] = { 3 , 4 , 5 , 6 , 7 };
List<Short> myList = Shorts.asList(arr);
System.out.println(myList);
}
}
|
Output :
[3, 4, 5, 6, 7]
Example 2 :
import com.google.common.primitives.Shorts;
import java.util.*;
class GFG {
public static void main(String[] args)
{
short [] arr = { 3 , 4 , 5 , 6 , 7 };
System.out.println(Shorts.indexOf(arr, ( short ) 5 ));
}
}
|
Output :
2
Example 3 :
import com.google.common.primitives.Shorts;
import java.util.*;
class GFG {
public static void main(String[] args)
{
short [] arr1 = { 3 , 4 , 5 };
short [] arr2 = { 6 , 7 };
short [] arr = Shorts.concat(arr1, arr2);
System.out.println(Arrays.toString(arr));
}
}
|
Output :
[3, 4, 5, 6, 7]
Example 4 :
import com.google.common.primitives.Shorts;
class GFG {
public static void main(String[] args)
{
short [] arr = { 3 , 4 , 5 , 6 , 7 };
System.out.println(Shorts.contains(arr, ( short ) 8 ));
System.out.println(Shorts.contains(arr, ( short ) 7 ));
}
}
|
output :
false
true
Example 5 :
import com.google.common.primitives.Shorts;
class GFG {
public static void main(String[] args)
{
short [] arr = { 3 , 4 , 5 , 6 , 7 };
System.out.println(Shorts.min(arr));
}
}
|
Output :
3
Example 6 :
import com.google.common.primitives.Shorts;
class GFG {
public static void main(String[] args)
{
short [] arr = { 3 , 4 , 5 , 6 , 7 };
System.out.println(Shorts.max(arr));
}
}
|
Output :
7