- Guava 教程
- Guava - 首页
- Guava - 概述
- Guava - 环境设置
- Guava - Optional 类
- Guava - Preconditions 类
- Guava - Ordering 类
- Guava - Objects 类
- Guava - Range 类
- Guava - Throwables 类
- Guava - 集合工具类
- Guava - 缓存工具类
- Guava - 字符串工具类
- Guava - 原生类型工具类
- Guava - 数学工具类
- Guava 有用资源
- Guava - 快速指南
- Guava - 有用资源
- Guava - 讨论
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