Guava – Longs Class

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

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.NoField & Description
1static int BYTESThe number of bytes required to represent a primitive long value.
2static long MAX_POWER_OF_TWOThe largest power of two that can be represented as a long.

Methods

Sr.NoMethod & Description
1static List<Long> asList(long… backingArray)Returns a fixed-size list backed by the specified array, similar to Arrays.asList(Object[]).
2static int compare(long a, long b)Compares the two specified long values.
3static long[] concat(long[]… arrays)Returns the values from each provided array combined into a single array.
4static boolean contains(long[] array, long target)Returns true if target is present as an element anywhere in array.
5static 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.
6static 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().
7static 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}).
8static int hashCode(long value)Returns a hash code for value; equal to the result of invoking ((Long) value).hashCode().
9static int indexOf(long[] array, long target)Returns the index of the first appearance of the value target in array.
10static 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.
11static String join(String separator, long… array)Returns a string containing the supplied long values separated by separator.
12static int lastIndexOf(long[] array, long target)Returns the index of the last appearance of the value target in array.
13static Comparator<long[]> lexicographicalComparator()Returns a comparator that compares two long arrays lexicographically.
14static long max(long… array)Returns the greatest value present in array.
15static long min(long… array)Returns the least value present in array.
16static Converter<String,Long> stringConverter()Returns a serializable converter object that converts between strings and longs using Long.decode(java.lang.String) and Long.toString().
17static 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().
18static 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().
19static 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 

Previous Page:-Click Here

Leave a Reply