BAEL-4494 | .toArray(new MyClass[0]) or .toArray(new MyClass[myList.size()])? (#10903)

* BAEL-4220 | A Guide to IllegalAccessError and when it happens

* BAEL-4220 | A Guide to IllegalAccessError and when it happens | fix tests

* BAEL-4220 | A Guide to IllegalAccessError and when it happens | fix tests

* BAEL-4220 | A Guide to IllegalAccessError and when it happens | BDD test names

* BAEL-4494 | .toArray(new MyClass[0]) or .toArray(new MyClass[myList.size()])?

* BAEL-4494 | .toArray(new MyClass[0]) or .toArray(new MyClass[myList.size()])?

Co-authored-by: root <anmoldeep0123@gmail.com>
This commit is contained in:
anmoldeep0123 2021-06-24 10:06:23 +05:30 committed by GitHub
parent af408b9830
commit c52cb1eaf3
2 changed files with 97 additions and 25 deletions

View File

@ -1,31 +1,31 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<artifactId>core-java-collections-4</artifactId>
<version>0.1.0-SNAPSHOT</version>
<name>core-java-collections-4</name>
<packaging>jar</packaging>
<parent>
<groupId>com.baeldung.core-java-modules</groupId>
<artifactId>core-java-modules</artifactId>
<version>0.0.1-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<artifactId>core-java-collections-4</artifactId>
<version>0.1.0-SNAPSHOT</version>
<name>core-java-collections-4</name>
<packaging>jar</packaging>
<dependencies>
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<version>${assertj.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
<parent>
<groupId>com.baeldung.core-java-modules</groupId>
<artifactId>core-java-modules</artifactId>
<version>0.0.1-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>
<properties>
<assertj.version>3.19.0</assertj.version>
</properties>
<dependencies>
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<version>${assertj.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
<properties>
<assertj.version>3.19.0</assertj.version>
</properties>
</project>

View File

@ -0,0 +1,72 @@
package com.baeldung.collections.toarraycomparison;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.TreeSet;
import java.util.concurrent.TimeUnit;
import org.openjdk.jmh.annotations.Benchmark;
import org.openjdk.jmh.annotations.BenchmarkMode;
import org.openjdk.jmh.annotations.Fork;
import org.openjdk.jmh.annotations.Measurement;
import org.openjdk.jmh.annotations.Mode;
import org.openjdk.jmh.annotations.OutputTimeUnit;
import org.openjdk.jmh.annotations.Param;
import org.openjdk.jmh.annotations.Scope;
import org.openjdk.jmh.annotations.Setup;
import org.openjdk.jmh.annotations.State;
import org.openjdk.jmh.annotations.Warmup;
@Warmup(iterations = 5, time = 1, timeUnit = TimeUnit.SECONDS)
@Measurement(iterations = 5, time = 1, timeUnit = TimeUnit.SECONDS)
@Fork(value = 3, jvmArgsAppend = { "-XX:+UseParallelGC", "-Xms4g", "-Xmx4g" })
@BenchmarkMode(Mode.AverageTime)
@OutputTimeUnit(TimeUnit.NANOSECONDS)
@State(Scope.Benchmark)
public class ToArrayBenchmark {
@Param({ "10", "10000", "10000000" })
private int size;
@Param({ "array-list", "tree-set" })
private String type;
private Collection<String> collection;
@Setup
public void setup() {
switch (type) {
case "array-list":
collection = new ArrayList<String>();
break;
case "tree-set":
collection = new TreeSet<String>();
break;
default:
throw new UnsupportedOperationException();
}
for (int i = 0; i < size; i++) {
collection.add(String.valueOf(i));
}
}
@Benchmark
public String[] zero_sized() {
return collection.toArray(new String[0]);
}
@Benchmark
public String[] pre_sized() {
return collection.toArray(new String[collection.size()]);
}
public static void main(String[] args) {
try {
org.openjdk.jmh.Main.main(args);
} catch (IOException e) {
e.printStackTrace();
}
}
}