[英]Converting an array of objects to an array of their primitive types
如果您有一组具有原始类型(例如 Byte、Integer、Char 等)的 Java 对象。 有没有一种巧妙的方法可以将其转换为原始类型的数组? 特别是可以在不必创建新数组并循环遍历内容的情况下完成此操作。
例如,给定
Integer[] array
将其转换为最简洁的方法是什么
int[] intArray
不幸的是,当在 Hibernate 和一些我们无法控制的第三方库之间进行交互时,这是我们必须经常做的事情。 看起来这将是一个很常见的操作,所以如果没有捷径,我会感到惊讶。
谢谢你的帮助!
Apache Commons Lang再次成为您的朋友。 他们提供了ArrayUtils.toPrimitive()来满足你的需求。 您可以指定处理空值的方式。
使用 Java 8 中引入的流可以做到这一点:
int[] intArray = Arrays.stream(array).mapToInt(Integer::intValue).toArray();
但是,目前只有int
、 long
和double
原始流。 如果您需要转换为另一种基本类型,例如byte
则无需外部库的最短方法是:
byte[] byteArray = new byte[array.length];
for(int i = 0; i < array.length; i++) byteArray[i] = array[i];
或者,如果需要,可以将 for 循环替换为流:
IntStream.range(0, array.length).forEach(i -> byteArray[i] = array[i]);
如果您的任何元素为null
所有这些都将抛出NullPointerException
。
不幸的是,Java 平台中没有任何东西可以做到这一点。 顺便说一句,您还需要显式处理Integer[]
数组中的null
元素(您将使用什么int
?)。
使用番石榴:
int[] intArray = Ints.toArray(Arrays.asList(array));
文档:
Arrays.asList
(核心 API)Ints.toArray
(番石榴)特别是可以在不必创建新数组并循环遍历内容的情况下完成此操作。
在 Java 中不能将 Integer 数组转换为 int(即不能更改数组元素的类型)。 因此,您必须创建一个新的 int[] 数组并将 Integer 对象的值复制到其中,或者您可以使用适配器:
class IntAdapter {
private Integer[] array;
public IntAdapter (Integer[] array) { this.array = array; }
public int get (int index) { return array[index].intValue(); }
}
这可以使您的代码更具可读性,并且 IntAdapter 对象将只消耗几个字节的内存。 适配器的一大优势是您可以在此处处理特殊情况:
class IntAdapter {
private Integer[] array;
public int nullValue = 0;
public IntAdapter (Integer[] array) { this.array = array; }
public int get (int index) {
return array[index] == null ? nullValue : array[index].intValue();
}
}
另一种解决方案是使用包含许多预定义适配器的Commons Primitives 。 在您的情况下,请查看ListIntList 。
或者如果你只做一次,就用简单的方法来做。 但是你还没有谈到 Integer!=null 的情况。
//array is the Integer array
int[] array2 = new int[array.length];
int i=0;
for (Integer integer : array) {
array2[i] = integer.intValue();
i++;
}
使用Dollar很简单:
Integer[] array = ...;
int[] primitiveArray = $(array).toIntArray();
这是所有原始类型的通用解决方案
/**
* Convert Collection to equivalent array of primitive type
* @param <S> [in] Object type of source collection
* @param tcls [in] class of the primitive element
* @param q [in] source collection
* @return Equivalent Array of tcls-elements, requires cast to "tcls"[]
*/
public static <S> Object asPrimitiveArray(Class<?> tcls, Collection<S> q)
{
int n = q.size();
Object res = Array.newInstance(tcls, n);
Iterator<S> i = q.iterator();
int j = 0;
while (i.hasNext())
{
Array.set(res, j++, i.next());
}
return res;
}
/**
* Convert Object array to equivalent array of primitive type
* @param <S> [in] Object type of source array
* @param tcls [in] class of the primitive element
* @param s [in] source array
* @return Equivalent Array of tcls-elements, requires cast to "tcls"[]
*/
public static <S> Object asPrimitiveArray(Class<?> tcls, S[] s)
{
return asPrimitiveArray(tcls, Arrays.asList(s));
}
整数到整数的转换
Integer[] a = ...
int[] t = (int[]) asPrimitiveArray(int.class, a);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.