Longs是基本類型long的實用工具類。
以下是com.google.common.primitives.Longs類的聲明:
@GwtCompatible
public final class Longs
? ?extends Object
字段
方法
繼承的方法
這個類繼承了以下類方法:java.lang.Object
使用所選擇的任何編輯器創建下面的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?
轉載自并發編程網-ifeve.com