💎一站式轻松地调用各大LLM模型接口,支持GPT4、智谱、星火、月之暗面及文生图 广告
# 7-原生类型 # 7-原生类型 [原文链接](http://code.google.com/p/guava-libraries/wiki/PrimitivesExplained) [译文链接](http://ifeve.com/google-guava-primitives) 译者:沈义扬,校对:丁一 ## 概述 Java的原生类型就是指基本类型:byte、short、int、long、float、double、char和boolean。 *在从Guava查找原生类型方法之前,可以先查查*[***Arrays***](http://docs.oracle.com/javase/1.5.0/docs/api/java/util/Arrays.html)*类,或者对应的基础类型包装类,如*[***Integer***](http://docs.oracle.com/javase/1.5.0/docs/api/java/lang/Integer.html)***。*** 原生类型不能当作对象或泛型的类型参数使用,这意味着许多通用方法都不能应用于它们。Guava提供了若干通用工具,包括原生类型数组与集合API的交互,原生类型和字节数组的相互转换,以及对某些原生类型的无符号形式的支持。 **原生类型****Guava\*\***工具类(都在****com.google.common.primitives****包**\*\*)**byte[`Bytes`](http://docs.guava-libraries.googlecode.com/git-history/release/javadoc/com/google/common/primitives/Bytes.html), [`SignedBytes`](http://docs.guava-libraries.googlecode.com/git-history/release/javadoc/com/google/common/primitives/SignedBytes.html), [`UnsignedBytes`](http://docs.guava-libraries.googlecode.com/git-history/release/javadoc/com/google/common/primitives/UnsignedBytes.html)short[`Shorts`](http://docs.guava-libraries.googlecode.com/git-history/release/javadoc/com/google/common/primitives/Shorts.html)int[`Ints`](http://docs.guava-libraries.googlecode.com/git-history/release/javadoc/com/google/common/primitives/Ints.html), [`UnsignedInteger`](http://docs.guava-libraries.googlecode.com/git-history/release/javadoc/com/google/common/primitives/UnsignedInteger.html), [`UnsignedInts`](http://docs.guava-libraries.googlecode.com/git-history/release/javadoc/com/google/common/primitives/UnsignedInts.html)long[`Longs`](http://docs.guava-libraries.googlecode.com/git-history/release/javadoc/com/google/common/primitives/Longs.html), [`UnsignedLong`](http://docs.guava-libraries.googlecode.com/git-history/release/javadoc/com/google/common/primitives/UnsignedLong.html), [`UnsignedLongs`](http://docs.guava-libraries.googlecode.com/git-history/release/javadoc/com/google/common/primitives/UnsignedLongs.html)float[`Floats`](http://docs.guava-libraries.googlecode.com/git-history/release/javadoc/com/google/common/primitives/Floats.html)double[`Doubles`](http://docs.guava-libraries.googlecode.com/git-history/release/javadoc/com/google/common/primitives/Doubles.html)char[`Chars`](http://docs.guava-libraries.googlecode.com/git-history/release/javadoc/com/google/common/primitives/Chars.html)boolean[`Booleans`](http://docs.guava-libraries.googlecode.com/git-history/release/javadoc/com/google/common/primitives/Booleans.html)Bytes工具类没有定义任何区分有符号和无符号字节的方法,而是把它们都放到了SignedBytes和UnsignedBytes工具类中,因为字节类型的符号性比起其它类型要略微含糊一些。 int和long的无符号形式方法在UnsignedInts和UnsignedLongs类中,但由于这两个类型的大多数用法都是有符号的,Ints和Longs类按照有符号形式处理方法的输入参数。 此外,Guava为int和long的无符号形式提供了包装类,即UnsignedInteger和UnsignedLong,以帮助你使用类型系统,以极小的性能消耗对有符号和无符号值进行强制转换。 在本章下面描述的方法签名中,我们用Wrapper表示JDK包装类,prim表示原生类型。(Prims表示相应的Guava工具类。) ## 原生类型数组工具 原生类型数组是处理原生类型集合的最有效方式(从内存和性能双方面考虑)。Guava为此提供了许多工具方法。 **方法签名****描述****类似方法****可用性**List<Wrapper> asList(prim… backingArray)把数组转为相应包装类的List[Arrays.asList](http://docs.oracle.com/javase/6/docs/api/java/util/Arrays.html#asList%28T...%29)符号无关\*prim\[\] toArray(Collection<Wrapper> collection)把集合拷贝为数组,和collection.toArray()一样线程安全[Collection.toArray()](http://docs.oracle.com/javase/6/docs/api/java/util/Collection.html#toArray%28%29)符号无关prim\[\] concat(prim\[\]… arrays)串联多个原生类型数组[Iterables.concat](http://docs.guava-libraries.googlecode.com/git-history/release/javadoc/com/google/common/collect/Iterables.html#concat%28java.lang.Iterable...%29)符号无关boolean contains(prim\[\] array, prim target)判断原生类型数组是否包含给定值[Collection.contains](http://docs.oracle.com/javase/6/docs/api/java/util/Collection.html#contains%28java.lang.Object%29)符号无关int indexOf(prim\[\] array, prim target)给定值在数组中首次出现处的索引,若不包含此值返回-1[List.indexOf](http://docs.oracle.com/javase/6/docs/api/java/util/List.html#indexOf%28java.lang.Object%29)符号无关int lastIndexOf(prim\[\] array, prim target)给定值在数组最后出现的索引,若不包含此值返回-1[List.lastIndexOf](http://docs.oracle.com/javase/6/docs/api/java/util/List.html#lastIndexOf%28java.lang.Object%29)符号无关prim min(prim… array)数组中最小的值[Collections.min](http://docs.oracle.com/javase/6/docs/api/java/util/Collections.html#min%28java.util.Collection%29)符号相关\*prim max(prim… array)数组中最大的值[Collections.max](http://docs.oracle.com/javase/6/docs/api/java/util/Collections.html#max%28java.util.Collection%29)符号相关String join(String separator, prim… array)把数组用给定分隔符连接为字符串[Joiner.on(separator).join](http://code.google.com/p/guava-libraries/wiki/StringsExplained#Joiner)符号相关Comparator<prim\[\]> lexicographicalComparator()按字典序比较原生类型数组的Comparator[Ordering.natural().lexicographical()](http://docs.guava-libraries.googlecode.com/git-history/release/javadoc/com/google/common/collect/Ordering.html#lexicographical%28%29)符号相关\*符号无关方法存在于Bytes, Shorts, Ints, Longs, Floats, Doubles, Chars, Booleans。而UnsignedInts, UnsignedLongs, SignedBytes, 或UnsignedBytes不存在。 \*符号相关方法存在于SignedBytes, UnsignedBytes, Shorts, Ints, Longs, Floats, Doubles, Chars, Booleans, UnsignedInts, UnsignedLongs。而Bytes不存在。 ## 通用工具方法 Guava为原生类型提供了若干JDK6没有的工具方法。但请注意,其中某些方法已经存在于JDK7中。 **方法签名****描述****可用性**int compare(prim a, prim b)传统的Comparator.compare方法,但针对原生类型。JDK7的原生类型包装类也提供这样的方法符号相关prim checkedCast(long value)把给定long值转为某一原生类型,若给定值不符合该原生类型,则抛出IllegalArgumentException仅适用于符号相关的整型\*prim saturatedCast(long value)把给定long值转为某一原生类型,若给定值不符合则使用最接近的原生类型值仅适用于符号相关的整型\*这里的整型包括byte, short, int, long。不包括char, boolean, float, 或double。 *\*\*译者注:不符合主要是指long值超出prim类型的范围,比如过大的long超出int范围。* 注:com.google.common.math.DoubleMath提供了舍入double的方法,支持多种舍入模式。相见第12章的”浮点数运算”。 ## 字节转换方法 Guava提供了若干方法,用来把原生类型按**大字节序**与字节数组相互转换。所有这些方法都是符号无关的,此外Booleans没有提供任何下面的方法。 **方法或字段签名****描述**int BYTES常量:表示该原生类型需要的字节数prim fromByteArray(byte\[\] bytes)使用字节数组的前Prims.BYTES个字节,按**大字节序**返回原生类型值;如果bytes.length <= Prims.BYTES,抛出IAEprim fromBytes(byte b1, …, byte bk)接受Prims.BYTES个字节参数,按**大字节序**返回原生类型值byte\[\] toByteArray(prim value)按**大字节序**返回value的字节数组## 无符号支持 JDK原生类型包装类提供了针对有符号类型的方法,而UnsignedInts和UnsignedLongs工具类提供了相应的无符号通用方法。UnsignedInts和UnsignedLongs直接处理原生类型:使用时,由你自己保证只传入了无符号类型的值。 此外,对int和long,Guava提供了无符号包装类([UnsignedInteger](http://docs.guava-libraries.googlecode.com/git-history/release/javadoc/com/google/common/primitives/UnsignedInteger.html)和[UnsignedLong](http://docs.guava-libraries.googlecode.com/git-history/release/javadoc/com/google/common/primitives/UnsignedLong.html)),来帮助你以极小的性能消耗,对有符号和无符号类型进行强制转换。 ### 无符号通用工具方法 JDK的原生类型包装类提供了有符号形式的类似方法。 **方法签名****说明**[`int UnsignedInts.parseUnsignedInt(String)`](http://docs.guava-libraries.googlecode.com/git-history/release/javadoc/com/%E2%80%A6ogle/common/primitives/UnsignedInts.html#parseUnsignedInt%28java.lang.String%29)[`long UnsignedLongs.parseUnsignedLong(String)`](http://docs.guava-libraries.googlecode.com/git-history/release/javadoc/com/%E2%80%A6le/common/primitives/UnsignedLongs.html#parseUnsignedLong%28java.lang.String%29)按无符号十进制解析字符串[`int UnsignedInts.parseUnsignedInt(String string, int radix)`](http://docs.guava-libraries.googlecode.com/git-history/release/javadoc/com/%E2%80%A6ogle/common/primitives/UnsignedInts.html#parseUnsignedInt%28java.lang.String,%20int%29)[`long UnsignedLongs.parseUnsignedLong(String string, int radix)`](http://docs.guava-libraries.googlecode.com/git-history/release/javadoc/com/%E2%80%A6le/common/primitives/UnsignedLongs.html#parseUnsignedLong%28java.lang.String%29)按无符号的特定进制解析字符串[`String UnsignedInts.toString(int)`](http://docs.guava-libraries.googlecode.com/git-history/release/javadoc/com/google/common/primitives/UnsignedInts.html#toString%28int%29)[`String UnsignedLongs.toString(long)`](http://docs.guava-libraries.googlecode.com/git-history/release/javadoc/com/google/common/primitives/UnsignedLongs.html#toString%28long%29)数字按无符号十进制转为字符串[`String UnsignedInts.toString(int value, int radix)`](http://docs.guava-libraries.googlecode.com/git-history/release/javadoc/com/google/common/primitives/UnsignedInts.html#toString%28int,%20int%29)[`String UnsignedLongs.toString(long value, int radix)`](http://docs.guava-libraries.googlecode.com/git-history/release/javadoc/com/google/common/primitives/UnsignedLongs.html#toString%28long,%20int%29)数字按无符号特定进制转为字符串### 无符号包装类 无符号包装类包含了若干方法,让使用和转换更容易。 **方法签名****说明**UnsignedPrim add(UnsignedPrim), subtract, multiply, divide, remainder简单算术运算UnsignedPrim valueOf(BigInteger)按给定BigInteger返回无符号对象,若BigInteger为负或不匹配,抛出IAEUnsignedPrim valueOf(long)按给定long返回无符号对象,若long为负或不匹配,抛出IAEUnsignedPrim asUnsigned(prim value)把给定的值当作无符号类型。例如,UnsignedInteger.asUnsigned(1<<31)的值为2<sup>31</sup>,尽管1<<31当作int时是负的BigInteger bigIntegerValue()用BigInteger返回该无符号对象的值toString(), toString(int radix)返回无符号值的字符串表示