将List的前n个元素放入数组的最快方法

时间:2021-10-27 21:20:37

What is the fastest way to get the first n elements of a list stored in an array?

获取存储在数组中的列表的前n个元素的最快方法是什么?

Considering this as the scenario:

将此视为场景:

int n = 10;
ArrayList<String> in = new ArrayList<>();
for(int i = 0; i < (n+10); i++)
  in.add("foobar");

Option 1:

选项1:

String[] out = new String[n];
for(int i = 0; i< n; i++)
    out[i]=in.get(i);

Option 2:

选项2:

String[] out = (String[]) (in.subList(0, n)).toArray();

Option 3: Is there a faster way? Maybe with Java8-streams?

选项3:有更快的方法吗?也许使用Java8-streams?

5 个解决方案

#1


6  

Option 1 Faster Than Option 2

Because Option 2 creates a new List reference, and then creates an n element array from the List (option 1 perfectly sizes the output array). However, first you need to fix the off by one bug. Use < (not <=). Like,

因为选项2创建一个新的List引用,然后从List创建一个n元素数组(选项1完美地调整输出数组的大小)。但是,首先你需要修复一个bug。使用<(不是<=)。喜欢,

String[] out = new String[n];
for(int i = 0; i < n; i++) {
    out[i] = in.get(i);
}

#2


13  

Assumption:

假设:

list - List<String>

list - 列出

Using Java 8 Streams,

使用Java 8 Streams,

  • to get first N elements from a list into a list,

    将列表中的前N个元素放入列表中,

    List<String> firstNElementsList = list.stream().limit(n).collect(Collectors.toList());

    List firstNElementsList = list.stream()。limit(n).collect(Collectors.toList());

  • to get first N elements from a list into an Array,

    将列表中的前N个元素转换为数组,

    String[] firstNElementsArray = list.stream().limit(n).collect(Collectors.toList()).toArray(new String[n]);

    String [] firstNElementsArray = list.stream()。limit(n).collect(Collectors.toList())。toArray(new String [n]);

#3


3  

It mostly depends on how big n is.

它主要取决于n的大小。

If n==0, nothing beats option#1 :)

如果n == 0,没有什么比选项#1更好:)

If n is very large, toArray(new String[n]) is faster.

如果n非常大,toArray(new String [n])会更快。

#4


-1  

Use: Arrays.copyOf(yourArray,n);

使用:Arrays.copyOf(yourArray,n);

#5


-1  

I use the built-in way:

我使用内置方式:

System.arraycopy(srcArray, srcBeginning, destArray, destBeginning, length);

#1


6  

Option 1 Faster Than Option 2

Because Option 2 creates a new List reference, and then creates an n element array from the List (option 1 perfectly sizes the output array). However, first you need to fix the off by one bug. Use < (not <=). Like,

因为选项2创建一个新的List引用,然后从List创建一个n元素数组(选项1完美地调整输出数组的大小)。但是,首先你需要修复一个bug。使用<(不是<=)。喜欢,

String[] out = new String[n];
for(int i = 0; i < n; i++) {
    out[i] = in.get(i);
}

#2


13  

Assumption:

假设:

list - List<String>

list - 列出

Using Java 8 Streams,

使用Java 8 Streams,

  • to get first N elements from a list into a list,

    将列表中的前N个元素放入列表中,

    List<String> firstNElementsList = list.stream().limit(n).collect(Collectors.toList());

    List firstNElementsList = list.stream()。limit(n).collect(Collectors.toList());

  • to get first N elements from a list into an Array,

    将列表中的前N个元素转换为数组,

    String[] firstNElementsArray = list.stream().limit(n).collect(Collectors.toList()).toArray(new String[n]);

    String [] firstNElementsArray = list.stream()。limit(n).collect(Collectors.toList())。toArray(new String [n]);

#3


3  

It mostly depends on how big n is.

它主要取决于n的大小。

If n==0, nothing beats option#1 :)

如果n == 0,没有什么比选项#1更好:)

If n is very large, toArray(new String[n]) is faster.

如果n非常大,toArray(new String [n])会更快。

#4


-1  

Use: Arrays.copyOf(yourArray,n);

使用:Arrays.copyOf(yourArray,n);

#5


-1  

I use the built-in way:

我使用内置方式:

System.arraycopy(srcArray, srcBeginning, destArray, destBeginning, length);