- Guava Tutorial
- Guava - Home
- Guava - Overview
- Guava - Environment Setup
- Guava - Optional Class
- Guava - Preconditions Class
- Guava - Ordering Class
- Guava - Objects Class
- Guava - Range Class
- Guava - Throwables Class
- Guava - Collections Utilities
- Guava - Caching Utilities
- Guava - String Utilities
- Guava - Primitive Utilities
- Guava - Math Utilities
- Guava Useful Resources
- Guava - Quick Guide
- Guava - Useful Resources
- Guava - Discussion
Guava - Longs Class
Longs is a utility class for primitive type long.
Class Declaration
Following is the declaration for com.google.common.primitives.Longs class −
@GwtCompatible public final class Longs extends Object
Fields
Sr.No | Field & Description |
---|---|
1 |
static int BYTES The number of bytes required to represent a primitive long value. |
2 |
static long MAX_POWER_OF_TWO The largest power of two that can be represented as a long. |
Methods
Sr.No | Method & Description |
---|---|
1 |
static List<Long> asList(long... backingArray) Returns a fixed-size list backed by the specified array, similar to Arrays.asList(Object[]). |
2 |
static int compare(long a, long b) Compares the two specified long values. |
3 |
static long[] concat(long[]... arrays) Returns the values from each provided array combined into a single array. |
4 |
static boolean contains(long[] array, long target) Returns true if target is present as an element anywhere in array. |
5 |
static long[] ensureCapacity(long[] array, int minLength, int padding) Returns an array containing the same values as array, but guaranteed to be of a specified minimum length. |
6 | static long fromByteArray(byte[] bytes) Returns the long value whose big-endian representation is stored in the first 8 bytes of bytes; equivalent to ByteBuffer.wrap(bytes).getLong(). |
7 |
static long fromBytes(byte b1, byte b2, byte b3, byte b4, byte b5, byte b6, byte b7, byte b8) Returns the long value whose byte representation is the given 8 bytes, in big-endian order; equivalent to Longs.fromByteArray(new byte[] {b1, b2, b3, b4, b5, b6, b7, b8}). |
8 |
static int hashCode(long value) Returns a hash code for value; equal to the result of invoking ((Long) value).hashCode(). |
9 |
static int indexOf(long[] array, long target) Returns the index of the first appearance of the value target in array. |
10 |
static int indexOf(long[] array, long[] target) Returns the start position of the first occurrence of the specified target within array, or -1 if there is no such occurrence. |
11 |
static String join(String separator, long... array) Returns a string containing the supplied long values separated by separator. |
12 |
static int lastIndexOf(long[] array, long target) Returns the index of the last appearance of the value target in array. |
13 |
static Comparator<long[]> lexicographicalComparator() Returns a comparator that compares two long arrays lexicographically. |
14 |
static long max(long... array) Returns the greatest value present in array. |
15 |
static long min(long... array) Returns the least value present in array. |
16 |
static Converter<String,Long> stringConverter() Returns a serializable converter object that converts between strings and longs using Long.decode(java.lang.String) and Long.toString(). |
17 |
static long[] toArray(Collection<? extends Number> collection) Returns an array containing each value of collection, converted to a long value in the manner of Number.longValue(). |
18 |
static byte[] toByteArray(long value) Returns a big-endian representation of value in an 8-element byte array; equivalent to ByteBuffer.allocate(8).putLong(value).array(). |
19 |
static Long tryParse(String string) Parses the specified string as a signed decimal long value. |
Methods Inherited
This class inherits methods from the following class −
- java.lang.Object
Example of Longs 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.Ints; import com.google.common.primitives.Longs; public class GuavaTester { public static void main(String args[]) { GuavaTester tester = new GuavaTester(); tester.testLongs(); } private void testLongs() { long[] longArray = {1,2,3,4,5,6,7,8,9}; //convert array of primitives to array of objects List<Long> objectArray = Longs.asList(longArray); System.out.println(objectArray.toString()); //convert array of objects to array of primitives longArray = Longs.toArray(objectArray); System.out.print("[ "); for(int i = 0; i< longArray.length ; i++) { System.out.print(longArray[i] + " "); } System.out.println("]"); //check if element is present in the list of primitives or not System.out.println("5 is in list? "+ Longs.contains(longArray, 5)); //Returns the minimum System.out.println("Min: " + Longs.min(longArray)); //Returns the maximum System.out.println("Max: " + Longs.max(longArray)); //get the byte array from an integer byte[] byteArray = Longs.toByteArray(20000); 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 0 0 0 0 0 0 78 32