简体   繁体   English

一种转换 List 的有效方法<Integer>无需迭代即可转换为 int[] ( array )

[英]An efficient way to convert List<Integer> to int[] ( array ) without iteration

  public static int[] convertListToArray(List<Integer> listResult) {
        int[] result = new int[listResult.size()];
        int i= 0;
        for (int num : listResult) {
            result[i++] = num;
        }
        return result;
    }

Is there an efficient way to convert List to array without iterating List explicitly ?有没有一种有效的方法可以在不显式迭代 List 的情况下将 List 转换为数组? Maybe it is possible by using methods like:也许可以使用以下方法:

Arrays.copyOf(int [] origin , int newLength );
System.arraycopy(Object src,  int  srcPos,
                                    Object dest, int destPos,
                                    int length);

I know that there is a solution described here .我知道这里描述一个解决方案。 However, I particularly interested in an efficient way of converting List<Integer> to int[]但是,我对将List<Integer>转换为int[]的有效方法特别感兴趣

Given the need to convert from Integer to int , I don't think you're going to find something more efficient than what you have, if I assume you're talking about runtime efficiency.鉴于需要从Integer转换为int ,如果我假设您在谈论运行时效率,我认为您不会找到比现有的更有效的东西。

You might find converting to Integer[] first and then looping might be more efficient (below), but you might not , too.可能会发现先转换为Integer[]然后循环可能更有效(见下文),但您也可能不会 You'd have to test it in your specific scenario and see.您必须在您的特定场景中对其进行测试并查看。

Here's that example:这是那个例子:

int size = listResult.size();
int[] result = new int[size];
Integer[] temp = listResult.toArray(new Integer[size]);
for (int n = 0; n < size; ++n) {
    result[n] = temp[n];
}

If efficiency is your primary concern, I think you can use your solution and make it more efficient by using an indexed for loop on the listResult if it is RandomAccess .如果效率是您的主要关注点,我认为您可以使用您的解决方案并通过在 listResult 上使用索引 for 循环(如果它是RandomAccess来提高效率。 However this makes the code much less readable, and you'd have to benchmark it for your use cases to see if it is more efficient.但是,这会使代码的可读性大大降低,您必须针对您的用例对其进行基准测试,以查看它是否更有效。

public static int[] convertListToArray(List<Integer> listResult) {
    int size = listResult.size();
    int[] result = new int[size];
    if (listResult instanceof RandomAccess)
    {
        for (int i = 0; i < size; i++)
        {
            result[i] = listResult.get(i);
        }
    }
    else
    {
        int i = 0;
        for (int num : listResult) {
            result[i++] = num;
        }
    }
    return result;
}

If you use Java 8 and would like to write less code you can use the Streams library.如果您使用 Java 8 并希望编写更少的代码,则可以使用 Streams 库。

List<Integer> list = Arrays.asList(1, 2, 3, 4, 5);
int[] array = list.stream().mapToInt(i -> i).toArray();

If you are open to using a third party library, you can Eclipse Collections as follows.如果您愿意使用第三方库,您可以按如下方式使用Eclipse Collections

MutableList<Integer> list = Lists.mutable.with(1, 2, 3, 4, 5);
int[] array = list.collectInt(i -> i).toArray();

The following is slightly more code, but is the most efficient solution I could come up with using Eclipse Collections.下面是稍微多一些的代码,但它是我使用 Eclipse Collections 能想出的最有效的解决方案。

MutableList<Integer> list = Lists.mutable.with(1, 2, 3, 4, 5);
int[] array = new int[list.size()];
list.forEachWithIndex((each, index) -> array[index] = each);

If you need to use the java.util.List interface, the ListIterate utility class can be used from Eclipse Collections.如果您需要使用java.util.List接口,则可以从 Eclipse Collections 使用ListIterate实用程序类。

List<Integer> list = Arrays.asList(1, 2, 3, 4, 5);
int[] array = new int[list.size()];
ListIterate.forEachWithIndex(list, (each, index) -> array[index] = each);

The ListIterate utility will use different iteration code for RandomAccess lists and non- RandomAccess lists. ListIterate实用程序将为RandomAccess列表和非RandomAccess列表使用不同的迭代代码。

The most efficient thing to do would be to change the List<Integer> to a MutableIntList in Eclipse Collections or another library that has support for primitive collections.最有效的做法是将List<Integer>更改为 Eclipse Collections 或其他支持原始集合的库中的MutableIntList

Note: I am a committer for Eclipse Collections.注意:我是 Eclipse Collections 的提交者。

In Java 8:在 Java 8 中:

int[] anArray = list.stream()
                    .filter(Objects::nonNull)
                    .mapToInt(Integer::intValue)
                    .toArray();

There is efficient way you could do this Java.有一种有效的方法可以执行此 Java。 However, this could open room for someone to create the generic function (depend on demand).但是,这可能为某人创建通用功能(取决于需求)打开空间。

Just like this sample i wrote, I suggest you do the same to the specific knowledge of your program.就像我写的这个示例一样,我建议您对程序的特定知识进行同样的操作。

    // Generic function to convert set to list
    public static <T> ArrayList<T> convertSetToList(Set<T> set)
    {
        // create an empty list
        ArrayList<T> list = new ArrayList<>();
        // push each element in the set into the list
        for (T t : set)
            list.add(t);

        // return the list
        return list;
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM