From bb34174aeb777da69ef019aefeb9b102d374f35f Mon Sep 17 00:00:00 2001 From: Carlos Grappa Date: Sat, 15 Aug 2020 17:59:34 -0300 Subject: [PATCH] BAEL 2881 (#9639) * BAEL-4154 IOException Too many open files * Add comment to explain GC dependency * Add HPPC, remove Colt & Trove * Add inplace iterators for fastutil and extra maps for HPPC Co-authored-by: Carlos Grappa --- .../core-java-collections-maps-2/pom.xml | 14 +--- .../map/primitives/PrimitiveMaps.java | 81 +++++++++++++------ 2 files changed, 61 insertions(+), 34 deletions(-) diff --git a/core-java-modules/core-java-collections-maps-2/pom.xml b/core-java-modules/core-java-collections-maps-2/pom.xml index a64a11c6ea..7c4ab19945 100644 --- a/core-java-modules/core-java-collections-maps-2/pom.xml +++ b/core-java-modules/core-java-collections-maps-2/pom.xml @@ -21,20 +21,15 @@ ${eclipse-collections.version} - net.sf.trove4j - trove4j - ${trove4j.version} + com.carrotsearch + hppc + ${hppc.version} it.unimi.dsi fastutil ${fastutil.version} - - colt - colt - ${colt.version} - org.apache.commons commons-lang3 @@ -69,9 +64,8 @@ 4.1 1.7.0 8.2.0 - 3.0.2 + 0.7.2 8.1.0 - 1.2.0 3.11.1 diff --git a/core-java-modules/core-java-collections-maps-2/src/main/java/com/baeldung/map/primitives/PrimitiveMaps.java b/core-java-modules/core-java-collections-maps-2/src/main/java/com/baeldung/map/primitives/PrimitiveMaps.java index 30bec12ccc..e53290f93a 100644 --- a/core-java-modules/core-java-collections-maps-2/src/main/java/com/baeldung/map/primitives/PrimitiveMaps.java +++ b/core-java-modules/core-java-collections-maps-2/src/main/java/com/baeldung/map/primitives/PrimitiveMaps.java @@ -1,29 +1,68 @@ package com.baeldung.map.primitives; -import cern.colt.map.AbstractIntDoubleMap; -import cern.colt.map.OpenIntDoubleHashMap; -import gnu.trove.map.TDoubleIntMap; -import gnu.trove.map.hash.TDoubleIntHashMap; +import com.carrotsearch.hppc.IntLongHashMap; +import com.carrotsearch.hppc.IntLongScatterMap; +import com.carrotsearch.hppc.IntObjectHashMap; +import com.carrotsearch.hppc.IntObjectMap; +import com.carrotsearch.hppc.IntObjectScatterMap; + import it.unimi.dsi.fastutil.ints.Int2BooleanMap; +import it.unimi.dsi.fastutil.ints.Int2BooleanMaps; import it.unimi.dsi.fastutil.ints.Int2BooleanOpenHashMap; import it.unimi.dsi.fastutil.ints.Int2BooleanSortedMap; import it.unimi.dsi.fastutil.ints.Int2BooleanSortedMaps; +import it.unimi.dsi.fastutil.objects.ObjectIterator; + import org.eclipse.collections.api.map.primitive.ImmutableIntIntMap; import org.eclipse.collections.api.map.primitive.MutableIntIntMap; import org.eclipse.collections.api.map.primitive.MutableObjectDoubleMap; import org.eclipse.collections.impl.factory.primitive.IntIntMaps; import org.eclipse.collections.impl.factory.primitive.ObjectDoubleMaps; +import static java.lang.String.format; + +import java.math.BigDecimal; + public class PrimitiveMaps { public static void main(String[] args) { + hppcMap(); eclipseCollectionsMap(); - troveMap(); - coltMap(); fastutilMap(); } + private static void hppcMap() { + //Regular maps + IntLongHashMap intLongHashMap = new IntLongHashMap(); + intLongHashMap.put(25,1L); + intLongHashMap.put(150,Long.MAX_VALUE); + intLongHashMap.put(1,0L); + + intLongHashMap.get(150); + + IntObjectMap intObjectMap = new IntObjectHashMap(); + intObjectMap.put(1, BigDecimal.valueOf(1)); + intObjectMap.put(2, BigDecimal.valueOf(2500)); + + BigDecimal value = intObjectMap.get(2); + + //Scatter maps + IntLongScatterMap intLongScatterMap = new IntLongScatterMap(); + intLongScatterMap.put(1, 1L); + intLongScatterMap.put(2, -2L); + intLongScatterMap.put(1000,0L); + + intLongScatterMap.get(1000); + + IntObjectScatterMap intObjectScatterMap = new IntObjectScatterMap(); + intObjectScatterMap.put(1, BigDecimal.valueOf(1)); + intObjectScatterMap.put(2, BigDecimal.valueOf(2500)); + + value = intObjectScatterMap.get(2); + } + + private static void fastutilMap() { Int2BooleanMap int2BooleanMap = new Int2BooleanOpenHashMap(); int2BooleanMap.put(1, true); @@ -32,13 +71,19 @@ public class PrimitiveMaps { boolean value = int2BooleanMap.get(1); - Int2BooleanSortedMap int2BooleanSorted = Int2BooleanSortedMaps.EMPTY_MAP; - } + //Lambda style iteration + Int2BooleanMaps.fastForEach(int2BooleanMap, entry -> { + System.out.println(String.format("Key: %d, Value: %b",entry.getIntKey(),entry.getBooleanValue())); + }); + + //Iterator based loop + ObjectIterator iterator = Int2BooleanMaps.fastIterator(int2BooleanMap); + while(iterator.hasNext()) { + Int2BooleanMap.Entry entry = iterator.next(); + System.out.println(String.format("Key: %d, Value: %b",entry.getIntKey(),entry.getBooleanValue())); + + } - private static void coltMap() { - AbstractIntDoubleMap map = new OpenIntDoubleHashMap(); - map.put(1, 4.5); - double value = map.get(1); } private static void eclipseCollectionsMap() { @@ -53,17 +98,5 @@ public class PrimitiveMaps { dObject.addToValue("stability", 0.8); } - private static void troveMap() { - double[] doubles = new double[] {1.2, 4.5, 0.3}; - int[] ints = new int[] {1, 4, 0}; - TDoubleIntMap doubleIntMap = new TDoubleIntHashMap(doubles, ints); - - doubleIntMap.put(1.2, 22); - doubleIntMap.put(4.5, 16); - - doubleIntMap.adjustValue(1.2, 1); - doubleIntMap.adjustValue(4.5, 4); - doubleIntMap.adjustValue(0.3, 7); - } }