Guava - Useful Classes
- Guava - Optional Class
- Guava - Preconditions Class
- Guava - Ordering Class
- Guava - Objects Class
- Guava - Range Class
- Guava - Throwables Class
- Guava - LoadingCache Interface
Guava - Collection Utilities
- Guava - Collections Utilities
- Guava - MultiSet Interface
- Guava - MultiMap Interface
- Guava - BiMap Interface
- Guava - Table Interface
Guava - String Utilities
- Guava - String Utilities
- Guava - Joiner class
- Guava - Splitter
- Guava - CharMatcher
- Guava - CaseFormat
Guava - Primitive Utilities
- Guava - Primitive Utilities
- Guava - Bytes
- Guava - Shorts
- Guava - Ints
- Guava - Longs
- Guava - Floats
- Guava - Doubles
- Guava - Chars
- Guava - Booleans
Guava - Math Utilities
Guava - Useful Resources
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
| 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. |
| 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 - Converting Array of Long to List and vice versa
GuavaTester.java
package com.tutorialspoint;
import java.util.List;
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("]");
}
}
Output
Run the GuavaTester and verify the output.
[1, 2, 3, 4, 5, 6, 7, 8, 9] [ 1 2 3 4 5 6 7 8 9 ]
Example - Checking Presence of Element in Array of Longs
GuavaTester.java
package com.tutorialspoint;
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};
//check if element is present in the list of primitives or not
System.out.println("5 is in list? "+ Longs.contains(longArray, 5L));
System.out.println("10 is in list? "+ Longs.contains(longArray, 10L));
}
}
Output
Run the GuavaTester and verify the output.
5 is in list? true 10 is in list? false
Example - Getting Minimum/Maximum from an Array of Longs
GuavaTester.java
package com.tutorialspoint;
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};
//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] + " ");
}
}
}
Output
Run the GuavaTester and verify the output.
Min: 1 Max: 9 0 0 0 0 0 0 78 32