我如何在Java中转换列表<整数>到int[] ?

我很困惑,因为List.toArray()实际上返回一个对象[],它既不能转换为Integer[]也不能转换为int[]。

现在我正在使用一个循环来这样做:

int[] toIntArray(List<Integer> list) {
  int[] ret = new int[list.size()];
  for(int i = 0; i < ret.length; i++)
    ret[i] = list.get(i);
  return ret;
}

有更好的办法吗?

这和上面的问题类似 如何在Java中将int[]转换为Integer[] ?


当前回答

在Java 8中添加流后,我们可以编写如下代码:

int[] example1 = list.stream().mapToInt(i->i).toArray();
// OR
int[] example2 = list.stream().mapToInt(Integer::intValue).toArray();

思维过程:

The simple Stream#toArray returns an Object[] array, so it is not what we want. Also, Stream#toArray(IntFunction<A[]> generator) doesn't do what we want, because the generic type A can't represent the primitive type int So it would be nice to have some stream which could handle the primitive type int instead of the wrapper Integer, because its toArray method will most likely also return an int[] array (returning something else like Object[] or even boxed Integer[] would be unnatural here). And fortunately Java 8 has such a stream which is IntStream So now the only thing we need to figure out is how to convert our Stream<Integer> (which will be returned from list.stream()) to that shiny IntStream. Quick searching in documentation of Stream while looking for methods which return IntStream points us to our solution which is mapToInt(ToIntFunction<? super T> mapper) method. All we need to do is provide a mapping from Integer to int. Since ToIntFunction is functional interface we can provide its instance via lambda or method reference. Anyway to convert Integer to int we can use Integer#intValue so inside mapToInt we can write: mapToInt( (Integer i) -> i.intValue() ) (or some may prefer: mapToInt(Integer::intValue).) But similar code can be generated using unboxing, since the compiler knows that the result of this lambda must be of type int (the lambda used in mapToInt is an implementation of the ToIntFunction interface which expects as body a method of type: int applyAsInt(T value) which is expected to return an int). So we can simply write: mapToInt((Integer i)->i) Also, since the Integer type in (Integer i) can be inferred by the compiler because List<Integer>#stream() returns a Stream<Integer>, we can also skip it which leaves us with mapToInt(i -> i)

其他回答

如果您只是将一个Integer映射到int,那么您应该考虑使用并行性,因为您的映射逻辑不依赖于其作用域之外的任何变量。

int[] arr = list.parallelStream().mapToInt(Integer::intValue).toArray();

要注意这一点

请注意,并行并不会自动地比串行执行操作快,尽管如果您有足够的数据和处理器核心,它可能会快。虽然聚合操作使您能够更容易地实现并行性,但您仍有责任确定应用程序是否适合并行性。


有两种方法将integer映射到它们的原始形式:

通过ToIntFunction。 mapToInt(整数::intValue) 通过lambda表达式显式开箱。 mapToInt(i -> i. intvalue ()) 通过隐式(自动)解盒lambda表达式。 mapToInt(i -> i)


给定一个空值的列表

List<Integer> list = Arrays.asList(1, 2, null, 4, 5);

这里有三个处理null的选项:

在映射之前过滤掉空值。 int[] arr = list.parallelStream().filter(对象::nonNull).mapToInt(Integer::intValue).toArray(); 将空值映射为默认值。 int[] arr = list.parallelStream()。映射(i -> i == null ?-1:i).mapToInt(Integer::intValue).toArray(); 在lambda表达式中处理null。 int[] arr = list.parallelStream()。mapToInt(i -> i == null ?-1: i.t intvalue ()).toArray();

也可以试试Dollar(检查这个修订版):

import static com.humaorie.dollar.Dollar.*
...

List<Integer> source = ...;
int[] ints = $(source).convert().toIntArray();

我建议您使用List<?来自Java集合API的>骨架实现。在这种特殊情况下,它似乎很有帮助:

package mypackage;

import java.util.AbstractList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

public class Test {

    // Helper method to convert int arrays into Lists
    static List<Integer> intArrayAsList(final int[] a) {
        if(a == null)
            throw new NullPointerException();
        return new AbstractList<Integer>() {

            @Override
            public Integer get(int i) {
                return a[i]; // Autoboxing
            }
            @Override
            public Integer set(int i, Integer val) {
                final int old = a[i];
                a[i] = val; // Auto-unboxing
                return old; // Autoboxing
            }
            @Override
            public int size() {
                return a.length;
            }
        };
    }

    public static void main(final String[] args) {
        int[] a = {1, 2, 3, 4, 5};
        Collections.reverse(intArrayAsList(a));
        System.out.println(Arrays.toString(a));
    }
}

小心装箱/拆箱的缺点。

这个简单的循环总是正确的!没有错误

  int[] integers = new int[myList.size()];
  for (int i = 0; i < integers.length; i++) {
      integers[i] = myList.get(i);
  }

下面是一个Java 8的单行代码:

public int[] toIntArray(List<Integer> intList){
    return intList.stream().mapToInt(Integer::intValue).toArray();
}