Guava - Longs 類



Longs 是一個用於基本型別 long 的工具類。

類宣告

以下是com.google.common.primitives.Longs類的宣告:

@GwtCompatible
public final class Longs
   extends Object

欄位

序號 欄位及描述
1

static int BYTES

表示一個基本型別 long 值所需的位元組數。

2

static long MAX_POWER_OF_TWO

可以表示為 long 的最大 2 的冪。

方法

序號 方法及描述
1

static List<Long> asList(long... backingArray)

返回一個由指定陣列支援的固定大小列表,類似於 Arrays.asList(Object[])。

2

static int compare(long a, long b)

比較兩個指定的 long 值。

3

static long[] concat(long[]... arrays)

返回來自每個提供的陣列組合成單個數組的值。

4

static boolean contains(long[] array, long target)

如果目標作為元素存在於陣列中的任何位置,則返回 true。

5

static long[] ensureCapacity(long[] array, int minLength, int padding)

返回一個包含與陣列相同值的陣列,但保證具有指定的最小長度。

6

static long fromByteArray(byte[] bytes)

返回其大端表示儲存在 bytes 的前 8 個位元組中的 long 值;等效於 ByteBuffer.wrap(bytes).getLong()。

7

static long fromBytes(byte b1, byte b2, byte b3, byte b4, byte b5, byte b6, byte b7, byte b8)

返回其位元組表示為給定 8 個位元組的 long 值,以大端順序;等效於 Longs.fromByteArray(new byte[] {b1, b2, b3, b4, b5, b6, b7, b8})。

8

static int hashCode(long value)

返回 value 的雜湊碼;等於呼叫 ((Long) value).hashCode() 的結果。

9

static int indexOf(long[] array, long target)

返回目標值在陣列中第一次出現的位置。

10

static int indexOf(long[] array, long[] target)

返回陣列中指定目標的第一次出現的起始位置,如果不存在則返回 -1。

11

static String join(String separator, long... array)

返回一個包含由分隔符分隔的提供的 long 值的字串。

12

static int lastIndexOf(long[] array, long target)

返回目標值在陣列中最後一次出現的位置。

13

static Comparator<long[]> lexicographicalComparator()

返回一個按字典順序比較兩個 long 陣列的比較器。

14

static long max(long... array)

返回陣列中存在的最大值。

15

static long min(long... array)

返回陣列中存在的最小值。

16

static Converter<String,Long> stringConverter()

返回一個可序列化的轉換器物件,它使用 Long.decode(java.lang.String) 和 Long.toString() 在字串和 longs 之間進行轉換。

17

static long[] toArray(Collection<? extends Number> collection)

返回一個包含集合中每個值的陣列,以 Number.longValue() 的方式轉換為 long 值。

18

static byte[] toByteArray(long value)

返回 value 的大端表示形式,該形式為 8 元素位元組陣列;等效於 ByteBuffer.allocate(8).putLong(value).array()。

19

static Long tryParse(String string)

將指定的字串解析為帶符號的十進位制 long 值。

繼承的方法

此類繼承自以下類的方法:

  • java.lang.Object

Longs 類的示例

使用您選擇的任何編輯器建立以下 Java 程式,例如在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] + " ");
      }
   }
}

驗證結果

使用javac編譯器編譯該類,如下所示:

C:\Guava>javac GuavaTester.java

現在執行 GuavaTester 以檢視結果。

C:\Guava>java GuavaTester

檢視結果。

[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 
guava_primitive_utilities.htm
廣告
© . All rights reserved.