为了将整数数组转换为整数列表,我尝试了以下几种方法:
初始化一个列表(类型为Integer),迭代数组并插入到列表中
使用Java 8流:
int[] ints = {1, 2, 3}; List<Integer> list = new ArrayList<Integer>(); Collections.addAll(list, Arrays.stream(ints).boxed().toArray(Integer[]::new));
从性能的角度来看,哪个更好?
为了将整数数组转换为整数列表,我尝试了以下几种方法:
初始化一个列表(类型为Integer),迭代数组并插入到列表中
使用Java 8流:
int[] ints = {1, 2, 3};
List<Integer> list = new ArrayList<Integer>();
Collections.addAll(list, Arrays.stream(ints).boxed().toArray(Integer[]::new));
从性能的角度来看,哪个更好?
List<Integer> list = Arrays.stream(ints).boxed().collect(Collectors.toList());
第二个代码应该与第一个代码的性能大致相同。
请注意,在这样一个小数组的情况下,不会有任何显著的差异。你应该尽量编写正确、可读、易于维护的代码,而不是关注性能。
只需尝试类似于
Arrays.asList(array)
如果您不想改变列表:
List<Integer> list = Arrays.asList(array)
但是如果您想修改它,可以使用以下方法:
List<Integer> list = new ArrayList<Integer>(Arrays.asList(ints));
或者只需像以下这样使用Java 8:
List<Integer> list = Arrays.stream(ints).collect(Collectors.toList());
Java9 引入了这个方法:
List<Integer> list = List.of(ints);
然而,这会返回一个不可变的列表,您无法添加元素。
要使其可变,需要执行以下操作:
List<Integer> list = new ArrayList<Integer>(List.of(ints));
int[] ints = {1, 2, 3};
MutableIntList intList = IntLists.mutable.with(ints);
List<Integer> list = intList.collect(Integer::valueOf);
IntArrayList
上的 collect
代码实际上是在做什么呢:public <V> MutableList<V> collect(IntToObjectFunction<? extends V> function)
{
return this.collect(function, FastList.newList(this.size));
}
public <V, R extends Collection<V>> R collect(IntToObjectFunction<? extends V> function,
R target)
{
for (int i = 0; i < this.size; i++)
{
target.add(function.valueOf(this.items[i]));
}
return target;
}
import org.eclipse.collections.api.list.primitive.IntList;
import org.eclipse.collections.impl.factory.primitive.IntLists;
import org.openjdk.jmh.annotations.Benchmark;
import org.openjdk.jmh.annotations.BenchmarkMode;
import org.openjdk.jmh.annotations.Fork;
import org.openjdk.jmh.annotations.Mode;
import org.openjdk.jmh.annotations.OutputTimeUnit;
import org.openjdk.jmh.annotations.Scope;
import org.openjdk.jmh.annotations.State;
import org.openjdk.jmh.runner.Runner;
import org.openjdk.jmh.runner.RunnerException;
import org.openjdk.jmh.runner.options.Options;
import org.openjdk.jmh.runner.options.OptionsBuilder;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
@State(Scope.Thread)
@BenchmarkMode(Mode.Throughput)
@OutputTimeUnit(TimeUnit.SECONDS)
@Fork(2)
public class IntegerArrayListFromIntArray
{
private int[] source = IntStream.range(0, 1000).toArray();
public static void main(String[] args) throws RunnerException
{
Options options = new OptionsBuilder().include(
".*" + IntegerArrayListFromIntArray.class.getSimpleName() + ".*")
.forks(2)
.mode(Mode.Throughput)
.timeUnit(TimeUnit.SECONDS)
.build();
new Runner(options).run();
}
@Benchmark
public List<Integer> jdkClassic()
{
List<Integer> list = new ArrayList<>(source.length);
for (int each : source)
{
list.add(each);
}
return list;
}
@Benchmark
public List<Integer> jdkStreams1()
{
List<Integer> list = new ArrayList<>(source.length);
Collections.addAll(list,
Arrays.stream(source).boxed().toArray(Integer[]::new));
return list;
}
@Benchmark
public List<Integer> jdkStreams2()
{
return Arrays.stream(source).boxed().collect(Collectors.toList());
}
@Benchmark
public IntList ecPrimitive()
{
return IntLists.immutable.with(source);
}
@Benchmark
public List<Integer> ecBoxed()
{
return IntLists.mutable.with(source).collect(Integer::valueOf);
}
}
ecPrimitive
基准测试中使用了ImmutableIntList
,因为Eclipse Collections中的MutableIntList
默认不会复制数组。它只是适配你提供的数组。这导致ecPrimitive
报告更大的数字,但误差非常大,因为它本质上只测量了单个对象创建的成本。# Run complete. Total time: 00:06:52
Benchmark Mode Cnt Score Error Units
IntegerArrayListFromIntArray.ecBoxed thrpt 40 191671.859 ± 2107.723 ops/s
IntegerArrayListFromIntArray.ecPrimitive thrpt 40 2311575.358 ± 9194.262 ops/s
IntegerArrayListFromIntArray.jdkClassic thrpt 40 138231.703 ± 1817.613 ops/s
IntegerArrayListFromIntArray.jdkStreams1 thrpt 40 87421.892 ± 1425.735 ops/s
IntegerArrayListFromIntArray.jdkStreams2 thrpt 40 103034.520 ± 1669.947 ops/s
Arrays.stream(ints).forEach(list::add);
这基本上是使用Java 8执行1(迭代数组)和2(使用Java 8)。(其中1和2指的是您最初的问题)
第一种方式更好,第二种方式需要更多的时间来创建一个新数组并转换为列表。
其中stateb是一个List'',bucket是一个二维数组
statesb= IntStream.of(bucket[j-1]).boxed().collect(Collectors.toList());
需要使用import java.util.stream.IntStream;
参见https://examples.javacodegeeks.com/core-java/java8-convert-array-list-example/
如果你处理的是String[]而不是int[],我们可以使用
ArrayList<String> list = new ArrayList<>();
list.addAll(Arrays.asList(StringArray));
int numeros[] = {4, 10, 7, 25, 60, 1}; List lista = IntStream.of(numeros).boxed().collect(Collectors.toList());
int数组numeros[] = {4, 10, 7, 25, 60, 1}; List列表 = IntStream.of(numeros).boxed().collect(Collectors.toList());