目录

Longs

Longs是原始类型long的实用程序类。

Class 声明 (Class Declaration)

以下是com.google.common.primitives.Longs类的声明 -

@GwtCompatible
public final class Longs
   extends Object

字段 (Fields)

Sr.No 领域和描述
1

static int BYTES

表示原始long值所需的字节数。

2

static long MAX_POWER_OF_TWO

两个最大的力量可以表示为长。

方法 (Methods)

Sr.No 方法和描述
1

static List《Long》 asList(long... backingArray)

返回由指定数组支持的固定大小列表,类似于Arrays.asList(Object [])。

2

static int compare(long a, long b)

比较两个指定的长值。

3

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

返回每个提供的数组组合成单个数组的值。

4

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

如果target作为数组中的任何元素存在,则返回true。

5

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

返回一个包含与array相同值的数组,但保证指定的最小长度。

6

static long fromByteArray(byte[] bytes)

返回其big-endian表示存储在前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)

以big-endian顺序返回其字节表示为给定8字节的long值; 相当于Longs.fromByteArray(new byte [] {b1,b2,b3,b4,b5,b6,b7,b8})。

8

static int hashCode(long value)

返回值的哈希码; 等于调用((长)值).hashCode()的结果。

9

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

返回数组中值target的第一次出现的索引。

10

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

返回数组中指定目标第一次出现的起始位置,如果不存在,则返回-1。

11

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

返回一个字符串,其中包含由separator分隔的提供的long值。

12

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

返回数组中值target的最后一次出现的索引。

13

static Comparator《long[]》 lexicographicalComparator()

返回一个比较器,按字典顺序比较两个长数组。

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()在字符串和long之间进行转换。

17

static long[] toArray(Collection《? extends Number》 collection)

返回一个包含collection的每个值的数组,以Number.longValue()的方式转换为long值。

18

static byte[] toByteArray(long value)

返回8元素字节数组中的big-endian值表示形式; 相当于ByteBuffer.allocate(8).putLong(value).array()。

19

static Long tryParse(String string)

将指定的字符串解析为带符号的十进制长值。

方法继承 (Methods Inherited)

该类继承以下类中的方法 -

  • java.lang.Object

Longs类的示例

使用您选择的任何编辑器在C:/》 Guava.创建以下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 
↑回到顶部↑
WIKI教程 @2018