From d83101854c35218ea54bbaa5f6ce6747d16d44bf Mon Sep 17 00:00:00 2001 From: mherbaghinyan Date: Fri, 18 Jan 2019 11:05:26 +0400 Subject: [PATCH 1/9] primitive collections libraries added --- core-java-arrays/pom.xml | 16 ++++ .../baeldung/array/PrimitiveCollections.java | 34 ++++++++ .../array/PrimitivesListPerformance.java | 77 +++++++++++++++++++ 3 files changed, 127 insertions(+) create mode 100644 core-java-arrays/src/main/java/com/baeldung/array/PrimitiveCollections.java create mode 100644 core-java-arrays/src/main/java/com/baeldung/array/PrimitivesListPerformance.java diff --git a/core-java-arrays/pom.xml b/core-java-arrays/pom.xml index d2d0453e87..6d4109804e 100644 --- a/core-java-arrays/pom.xml +++ b/core-java-arrays/pom.xml @@ -52,6 +52,22 @@ spring-web ${springframework.spring-web.version} + + + net.sf.trove4j + trove4j + 3.0.2 + + + it.unimi.dsi + fastutil + 8.1.0 + + + colt + colt + 1.2.0 + diff --git a/core-java-arrays/src/main/java/com/baeldung/array/PrimitiveCollections.java b/core-java-arrays/src/main/java/com/baeldung/array/PrimitiveCollections.java new file mode 100644 index 0000000000..115c5fe5c3 --- /dev/null +++ b/core-java-arrays/src/main/java/com/baeldung/array/PrimitiveCollections.java @@ -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 integers = Ints.asList(primitives); + + int[] primitive = Ints.toArray(Arrays.asList(1, 2, 3, 4, 5)); + System.out.println(Arrays.toString(primitive)); + } +} diff --git a/core-java-arrays/src/main/java/com/baeldung/array/PrimitivesListPerformance.java b/core-java-arrays/src/main/java/com/baeldung/array/PrimitivesListPerformance.java new file mode 100644 index 0000000000..9db3a75574 --- /dev/null +++ b/core-java-arrays/src/main/java/com/baeldung/array/PrimitivesListPerformance.java @@ -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 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(); + } +} From c713ddee6da9f3b3efc13a0b1935582c2581fee0 Mon Sep 17 00:00:00 2001 From: mherbaghinyan Date: Mon, 21 Jan 2019 09:26:47 +0400 Subject: [PATCH 2/9] trove list --- .../java/com/baeldung/array/PrimitiveCollections.java | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/core-java-arrays/src/main/java/com/baeldung/array/PrimitiveCollections.java b/core-java-arrays/src/main/java/com/baeldung/array/PrimitiveCollections.java index 115c5fe5c3..b991dcb598 100644 --- a/core-java-arrays/src/main/java/com/baeldung/array/PrimitiveCollections.java +++ b/core-java-arrays/src/main/java/com/baeldung/array/PrimitiveCollections.java @@ -2,6 +2,7 @@ package com.baeldung.array; import com.google.common.primitives.ImmutableIntArray; import com.google.common.primitives.Ints; +import gnu.trove.list.TIntList; import gnu.trove.list.array.TIntArrayList; import java.util.Arrays; @@ -13,13 +14,16 @@ public class PrimitiveCollections { int[] primitives = new int[] {5, 10, 0, 2}; - guavaPrimitives(primitives); + //guavaPrimitives(primitives); trovePrimitives(primitives); } private static void trovePrimitives(int[] primitives) { - TIntArrayList list = new TIntArrayList(primitives); + TIntList tList = new TIntArrayList(primitives); + tList.reverse(); + System.out.println(tList); + System.out.println(tList.size()); } private static void guavaPrimitives(int[] primitives) { From 81f23992cb19ef7860cc4faa57df167a5d6d8ecd Mon Sep 17 00:00:00 2001 From: mherbaghinyan Date: Mon, 21 Jan 2019 13:08:09 +0400 Subject: [PATCH 3/9] primitive collections --- .../baeldung/array/PrimitiveCollections.java | 24 +++++++------------ 1 file changed, 9 insertions(+), 15 deletions(-) diff --git a/core-java-arrays/src/main/java/com/baeldung/array/PrimitiveCollections.java b/core-java-arrays/src/main/java/com/baeldung/array/PrimitiveCollections.java index b991dcb598..85f4dd2498 100644 --- a/core-java-arrays/src/main/java/com/baeldung/array/PrimitiveCollections.java +++ b/core-java-arrays/src/main/java/com/baeldung/array/PrimitiveCollections.java @@ -2,8 +2,6 @@ package com.baeldung.array; import com.google.common.primitives.ImmutableIntArray; import com.google.common.primitives.Ints; -import gnu.trove.list.TIntList; -import gnu.trove.list.array.TIntArrayList; import java.util.Arrays; import java.util.List; @@ -14,25 +12,21 @@ public class PrimitiveCollections { int[] primitives = new int[] {5, 10, 0, 2}; - //guavaPrimitives(primitives); - - trovePrimitives(primitives); + guavaPrimitives(primitives); } - private static void trovePrimitives(int[] primitives) { - TIntList tList = new TIntArrayList(primitives); - tList.reverse(); - System.out.println(tList); - System.out.println(tList.size()); - } private static void guavaPrimitives(int[] primitives) { - ImmutableIntArray list = ImmutableIntArray.builder().addAll(primitives).build(); + ImmutableIntArray immutableIntArray = ImmutableIntArray.builder().addAll(primitives).build(); + System.out.println(immutableIntArray); - List integers = Ints.asList(primitives); + List list = Ints.asList(primitives); - int[] primitive = Ints.toArray(Arrays.asList(1, 2, 3, 4, 5)); - System.out.println(Arrays.toString(primitive)); + int[] primitiveArray = Ints.toArray(list); + + int[] concatenated = Ints.concat(primitiveArray, primitives); + + System.out.println(Arrays.toString(concatenated)); } } From ae344b37ec7d7c0ed2cb0141e96c9811ad4eea71 Mon Sep 17 00:00:00 2001 From: mherbaghinyan Date: Fri, 25 Jan 2019 09:51:15 +0400 Subject: [PATCH 4/9] move the code into core-java-collections-lists module --- core-java-arrays/pom.xml | 16 ---------- core-java-collections-list/pom.xml | 29 +++++++++++++++++++ .../list/primitive}/PrimitiveCollections.java | 2 +- .../primitive}/PrimitivesListPerformance.java | 3 +- 4 files changed, 31 insertions(+), 19 deletions(-) rename {core-java-arrays/src/main/java/com/baeldung/array => core-java-collections-list/src/main/java/com/baeldung/list/primitive}/PrimitiveCollections.java (95%) rename {core-java-arrays/src/main/java/com/baeldung/array => core-java-collections-list/src/main/java/com/baeldung/list/primitive}/PrimitivesListPerformance.java (97%) diff --git a/core-java-arrays/pom.xml b/core-java-arrays/pom.xml index 6d4109804e..d2d0453e87 100644 --- a/core-java-arrays/pom.xml +++ b/core-java-arrays/pom.xml @@ -52,22 +52,6 @@ spring-web ${springframework.spring-web.version} - - - net.sf.trove4j - trove4j - 3.0.2 - - - it.unimi.dsi - fastutil - 8.1.0 - - - colt - colt - 1.2.0 - diff --git a/core-java-collections-list/pom.xml b/core-java-collections-list/pom.xml index ee99e470d0..bc71882683 100644 --- a/core-java-collections-list/pom.xml +++ b/core-java-collections-list/pom.xml @@ -36,6 +36,33 @@ ${lombok.version} provided + + + org.openjdk.jmh + jmh-core + ${jmh-core.version} + + + org.openjdk.jmh + jmh-generator-annprocess + ${jmh-generator-annprocess.version} + + + + net.sf.trove4j + trove4j + 3.0.2 + + + it.unimi.dsi + fastutil + 8.1.0 + + + colt + colt + 1.2.0 + @@ -44,5 +71,7 @@ 1.7.0 3.11.1 1.16.12 + 1.19 + 1.19 diff --git a/core-java-arrays/src/main/java/com/baeldung/array/PrimitiveCollections.java b/core-java-collections-list/src/main/java/com/baeldung/list/primitive/PrimitiveCollections.java similarity index 95% rename from core-java-arrays/src/main/java/com/baeldung/array/PrimitiveCollections.java rename to core-java-collections-list/src/main/java/com/baeldung/list/primitive/PrimitiveCollections.java index 85f4dd2498..d6da32d899 100644 --- a/core-java-arrays/src/main/java/com/baeldung/array/PrimitiveCollections.java +++ b/core-java-collections-list/src/main/java/com/baeldung/list/primitive/PrimitiveCollections.java @@ -1,4 +1,4 @@ -package com.baeldung.array; +package com.baeldung.list.primitive; import com.google.common.primitives.ImmutableIntArray; import com.google.common.primitives.Ints; diff --git a/core-java-arrays/src/main/java/com/baeldung/array/PrimitivesListPerformance.java b/core-java-collections-list/src/main/java/com/baeldung/list/primitive/PrimitivesListPerformance.java similarity index 97% rename from core-java-arrays/src/main/java/com/baeldung/array/PrimitivesListPerformance.java rename to core-java-collections-list/src/main/java/com/baeldung/list/primitive/PrimitivesListPerformance.java index 9db3a75574..f57c8d65a6 100644 --- a/core-java-arrays/src/main/java/com/baeldung/array/PrimitivesListPerformance.java +++ b/core-java-collections-list/src/main/java/com/baeldung/list/primitive/PrimitivesListPerformance.java @@ -1,4 +1,4 @@ -package com.baeldung.array; +package com.baeldung.list.primitive; import it.unimi.dsi.fastutil.ints.IntArrayList; import gnu.trove.list.array.TIntArrayList; @@ -9,7 +9,6 @@ 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) From 2f5ef3480b97cea30ec57dafb9370a114f9f8b18 Mon Sep 17 00:00:00 2001 From: mherbaghinyan Date: Fri, 25 Jan 2019 10:54:59 +0400 Subject: [PATCH 5/9] remove Ints examples --- .../com/baeldung/list/primitive/PrimitiveCollections.java | 8 -------- 1 file changed, 8 deletions(-) diff --git a/core-java-collections-list/src/main/java/com/baeldung/list/primitive/PrimitiveCollections.java b/core-java-collections-list/src/main/java/com/baeldung/list/primitive/PrimitiveCollections.java index d6da32d899..5a8ddc0acd 100644 --- a/core-java-collections-list/src/main/java/com/baeldung/list/primitive/PrimitiveCollections.java +++ b/core-java-collections-list/src/main/java/com/baeldung/list/primitive/PrimitiveCollections.java @@ -20,13 +20,5 @@ public class PrimitiveCollections { ImmutableIntArray immutableIntArray = ImmutableIntArray.builder().addAll(primitives).build(); System.out.println(immutableIntArray); - - List list = Ints.asList(primitives); - - int[] primitiveArray = Ints.toArray(list); - - int[] concatenated = Ints.concat(primitiveArray, primitives); - - System.out.println(Arrays.toString(concatenated)); } } From fd70f654b54e3ac59a9477809aed14c833ec15fa Mon Sep 17 00:00:00 2001 From: mherbaghinyan Date: Sun, 27 Jan 2019 17:45:34 +0400 Subject: [PATCH 6/9] primitive list collections --- .../list/primitive/PrimitiveCollections.java | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/core-java-collections-list/src/main/java/com/baeldung/list/primitive/PrimitiveCollections.java b/core-java-collections-list/src/main/java/com/baeldung/list/primitive/PrimitiveCollections.java index 5a8ddc0acd..50f372e9c9 100644 --- a/core-java-collections-list/src/main/java/com/baeldung/list/primitive/PrimitiveCollections.java +++ b/core-java-collections-list/src/main/java/com/baeldung/list/primitive/PrimitiveCollections.java @@ -2,6 +2,8 @@ package com.baeldung.list.primitive; import com.google.common.primitives.ImmutableIntArray; import com.google.common.primitives.Ints; +import gnu.trove.list.array.TIntArrayList; +import it.unimi.dsi.fastutil.ints.IntArrayList; import java.util.Arrays; import java.util.List; @@ -13,6 +15,18 @@ public class PrimitiveCollections { int[] primitives = new int[] {5, 10, 0, 2}; guavaPrimitives(primitives); + + TIntArrayList tList = new TIntArrayList(primitives); + + cern.colt.list.IntArrayList coltList = new cern.colt.list.IntArrayList(primitives); + + IntArrayList fastUtilList = new IntArrayList(primitives); + + System.out.println(tList); + + System.out.println(coltList); + + System.out.println(fastUtilList); } From 44d5be301ce9e69b69f4ef632250f6b8f07cd8c0 Mon Sep 17 00:00:00 2001 From: mherbaghinyan Date: Mon, 28 Jan 2019 15:15:05 +0400 Subject: [PATCH 7/9] remove performance test --- .../primitive/PrimitivesListPerformance.java | 76 ------------------- 1 file changed, 76 deletions(-) delete mode 100644 core-java-collections-list/src/main/java/com/baeldung/list/primitive/PrimitivesListPerformance.java diff --git a/core-java-collections-list/src/main/java/com/baeldung/list/primitive/PrimitivesListPerformance.java b/core-java-collections-list/src/main/java/com/baeldung/list/primitive/PrimitivesListPerformance.java deleted file mode 100644 index f57c8d65a6..0000000000 --- a/core-java-collections-list/src/main/java/com/baeldung/list/primitive/PrimitivesListPerformance.java +++ /dev/null @@ -1,76 +0,0 @@ -package com.baeldung.list.primitive; - -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.concurrent.TimeUnit; - -@BenchmarkMode(Mode.AverageTime) -@OutputTimeUnit(TimeUnit.NANOSECONDS) -@Warmup(iterations = 10) -public class PrimitivesListPerformance { - - @State(Scope.Thread) - public static class Initialize { - - List 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(); - } -} From d944b67c29a56d83ea31603238aa599de7cb212e Mon Sep 17 00:00:00 2001 From: mherbaghinyan Date: Mon, 28 Jan 2019 15:23:01 +0400 Subject: [PATCH 8/9] remove JMH dependency --- core-java-collections-list/pom.xml | 13 ------------- 1 file changed, 13 deletions(-) diff --git a/core-java-collections-list/pom.xml b/core-java-collections-list/pom.xml index bc71882683..a7e711088a 100644 --- a/core-java-collections-list/pom.xml +++ b/core-java-collections-list/pom.xml @@ -37,17 +37,6 @@ provided - - org.openjdk.jmh - jmh-core - ${jmh-core.version} - - - org.openjdk.jmh - jmh-generator-annprocess - ${jmh-generator-annprocess.version} - - net.sf.trove4j trove4j @@ -71,7 +60,5 @@ 1.7.0 3.11.1 1.16.12 - 1.19 - 1.19 From ca98fbb7062c13129ee3193b679a64ad9da63f84 Mon Sep 17 00:00:00 2001 From: mherbaghinyan Date: Thu, 31 Jan 2019 16:18:59 +0400 Subject: [PATCH 9/9] Added Stream Api --- .../list/primitive/PrimitiveCollections.java | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/core-java-collections-list/src/main/java/com/baeldung/list/primitive/PrimitiveCollections.java b/core-java-collections-list/src/main/java/com/baeldung/list/primitive/PrimitiveCollections.java index 50f372e9c9..01372763e9 100644 --- a/core-java-collections-list/src/main/java/com/baeldung/list/primitive/PrimitiveCollections.java +++ b/core-java-collections-list/src/main/java/com/baeldung/list/primitive/PrimitiveCollections.java @@ -7,15 +7,20 @@ import it.unimi.dsi.fastutil.ints.IntArrayList; import java.util.Arrays; import java.util.List; +import java.util.OptionalDouble; +import java.util.function.IntPredicate; +import java.util.stream.IntStream; public class PrimitiveCollections { public static void main(String[] args) { - int[] primitives = new int[] {5, 10, 0, 2}; + int[] primitives = new int[] {5, 10, 0, 2, -8}; guavaPrimitives(primitives); + intStream(primitives); + TIntArrayList tList = new TIntArrayList(primitives); cern.colt.list.IntArrayList coltList = new cern.colt.list.IntArrayList(primitives); @@ -29,6 +34,15 @@ public class PrimitiveCollections { System.out.println(fastUtilList); } + private static void intStream(int[] primitives) { + + IntStream stream = IntStream.of(5, 10, 0, 2, -8); + + IntStream newStream = IntStream.of(primitives); + + OptionalDouble average = stream.filter(i -> i > 0).average(); + } + private static void guavaPrimitives(int[] primitives) {