primitive collections libraries added

This commit is contained in:
mherbaghinyan 2019-01-18 11:05:26 +04:00
parent 68f1cb23dd
commit d83101854c
3 changed files with 127 additions and 0 deletions

View File

@ -52,6 +52,22 @@
<artifactId>spring-web</artifactId> <artifactId>spring-web</artifactId>
<version>${springframework.spring-web.version}</version> <version>${springframework.spring-web.version}</version>
</dependency> </dependency>
<dependency>
<groupId>net.sf.trove4j</groupId>
<artifactId>trove4j</artifactId>
<version>3.0.2</version>
</dependency>
<dependency>
<groupId>it.unimi.dsi</groupId>
<artifactId>fastutil</artifactId>
<version>8.1.0</version>
</dependency>
<dependency>
<groupId>colt</groupId>
<artifactId>colt</artifactId>
<version>1.2.0</version>
</dependency>
</dependencies> </dependencies>
<build> <build>

View File

@ -0,0 +1,34 @@
package com.baeldung.array;
import com.google.common.primitives.ImmutableIntArray;
import com.google.common.primitives.Ints;
import gnu.trove.list.array.TIntArrayList;
import java.util.Arrays;
import java.util.List;
public class PrimitiveCollections {
public static void main(String[] args) {
int[] primitives = new int[] {5, 10, 0, 2};
guavaPrimitives(primitives);
trovePrimitives(primitives);
}
private static void trovePrimitives(int[] primitives) {
TIntArrayList list = new TIntArrayList(primitives);
}
private static void guavaPrimitives(int[] primitives) {
ImmutableIntArray list = ImmutableIntArray.builder().addAll(primitives).build();
List<Integer> integers = Ints.asList(primitives);
int[] primitive = Ints.toArray(Arrays.asList(1, 2, 3, 4, 5));
System.out.println(Arrays.toString(primitive));
}
}

View File

@ -0,0 +1,77 @@
package com.baeldung.array;
import it.unimi.dsi.fastutil.ints.IntArrayList;
import gnu.trove.list.array.TIntArrayList;
import org.openjdk.jmh.annotations.*;
import org.openjdk.jmh.runner.Runner;
import org.openjdk.jmh.runner.options.Options;
import org.openjdk.jmh.runner.options.OptionsBuilder;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.concurrent.TimeUnit;
@BenchmarkMode(Mode.AverageTime)
@OutputTimeUnit(TimeUnit.NANOSECONDS)
@Warmup(iterations = 10)
public class PrimitivesListPerformance {
@State(Scope.Thread)
public static class Initialize {
List<Integer> arrayList = new ArrayList<>();
TIntArrayList tList = new TIntArrayList();
cern.colt.list.IntArrayList coltList = new cern.colt.list.IntArrayList();
IntArrayList fastUtilList = new IntArrayList();
int getValue = 10;
final int iterations = 100000;
@Setup(Level.Trial)
public void setUp() {
for (int i = 0; i < iterations; i++) {
arrayList.add(i);
tList.add(i);
coltList.add(i);
fastUtilList.add(i);
}
arrayList.add(getValue);
tList.add(getValue);
coltList.add(getValue);
fastUtilList.add(getValue);
}
}
@Benchmark
public boolean containsArrayList(PrimitivesListPerformance.Initialize state) {
return state.arrayList.contains(state.getValue);
}
@Benchmark
public boolean containsTIntList(PrimitivesListPerformance.Initialize state) {
return state.tList.contains(state.getValue);
}
@Benchmark
public boolean containsColtIntList(PrimitivesListPerformance.Initialize state) {
return state.coltList.contains(state.getValue);
}
@Benchmark
public boolean containsFastUtilIntList(PrimitivesListPerformance.Initialize state) {
return state.fastUtilList.contains(state.getValue);
}
public static void main(String[] args) throws Exception {
Options options = new OptionsBuilder()
.include(PrimitivesListPerformance.class.getSimpleName()).threads(1)
.forks(1).shouldFailOnError(true)
.shouldDoGC(true)
.jvmArgs("-server").build();
new Runner(options).run();
}
}