How to break a HashMap in Java

I was wondering if it is possible to split the HashMap into smaller sub-maps.

In my case, I have a HashMap of 100 elements, and I would like to create 2 (or more) smaller HashMaps from the original, the first of which contains entries from 0 to 49, the second - entries from 50 to 99.

Map <Integer, Integer> bigMap = new HashMap <Integer, Integer>(); //should contains entries from 0 to 49 of 'bigMap' Map <Integer, Integer> smallMap1 = new HashMap <Integer, Integer>(); //should contains entries from 50 to 99 of 'bigMap' Map <Integer, Integer> smallMap2 = new HashMap <Integer, Integer>(); 

Any suggestions? Many thanks!

+7
source share
7 answers

Do you need to use HashMap ?

TreeMap really good for this kind of thing. Here is an example.

 TreeMap<Integer, Integer> sorted = new TreeMap<Integer, Integer>(bigMap); SortedMap<Integer, Integer> zeroToFortyNine = sorted.subMap(0, 50); SortedMap<Integer, Integer> fiftyToNinetyNine = sorted.subMap(50, 100); 
+12
source

You basically need to bigMap over the entries in bigMap and decide whether to add them to smallMap1 or smallMap2 .

+3
source

Since the HashMap is unordered (entries can come in any order), it makes no sense to precisely separate it. We can just use a variable boolean flag.

 boolean b = false; for (Map.Entry e: bigMap.entrySet()) { if (b) smallMap1.put(e.getKey(), e.getValue()); else smallMap2.put(e.getKey(), e.getValue()); b = !b; } 
+3
source

Navigate bigMap with for (Entry<Integer, Integer> entry : bigMap.entrySet()) and increase i to check if you need to add an entry on the first small map or the second.

+1
source

Here is the solution with SortedMap:

 public static <K, V> List<SortedMap<K, V>> splitMap(final SortedMap<K, V> map, final int size) { List<K> keys = new ArrayList<>(map.keySet()); List<SortedMap<K, V>> parts = new ArrayList<>(); final int listSize = map.size(); for (int i = 0; i < listSize; i += size) { if (i + size < listSize) { parts.add(map.subMap(keys.get(i), keys.get(i + size))); } else { parts.add(map.tailMap(keys.get(i))); } } return parts; } 
+1
source
 for (Map.Entry<Integer,Integer> entry : bigMap.entrySet()) { // ... } 

- The fastest way to iterate over the original map. Then you can use Map.Entry to determine which new map will be filled.

0
source

This was one of the functions that performed my work, I hope that it will be useful to others. This works regardless of the object / primitive stored as the key.

The TreeMap approach proposed above will only work if the keys are primitives, ordered, and the exact sequence of the index.

  public List<Map<Integer, EnrichmentRecord>> splitMap(Map<Integer, EnrichmentRecord> enrichmentFieldsMap, int splitSize) { float mapSize = enrichmentFieldsMap.size(); float splitFactorF = splitSize; float actualNoOfBatches = (mapSize / splitFactorF); double noOfBatches = Math.ceil(actualNoOfBatches); List<Map<Integer, EnrichmentRecord>> listOfMaps = new ArrayList<>(); List<List<Integer>> listOfListOfKeys = new ArrayList<>(); int startIndex = 0; int endIndex = splitSize; Set<Integer> keys = enrichmentFieldsMap.keySet(); List<Integer> keysAsList = new ArrayList<>(); keysAsList.addAll(keys); /* * Split the keys as a list of keys, * For each key sub list add to a Primary List - listOfListOfKeys */ for (int i = 0; i < noOfBatches; i++) { listOfListOfKeys.add(keysAsList.subList(startIndex, endIndex)); startIndex = endIndex; endIndex = (int) (((endIndex + splitSize) > mapSize) ? mapSize : (endIndex + splitSize)); } /** * For Each list of keys, prepare a map * **/ for(List<Integer> keyList: listOfListOfKeys){ Map<Integer,EnrichmentRecord> subMap = new HashMap<>(); for(Integer key: keyList){ subMap.put(key,enrichmentFieldsMap.get(key)); } listOfMaps.add(subMap); } return listOfMaps; } 
0
source

All Articles