我想知道是否可以将HashMap拆分为更小的子地图。
在我的情况下,我有一个100个元素的HashMap,我想从原始的HashMap创建2个(或更多)小HashMaps,第一个包含从0到49的条目,第二个包含从50到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>();
有什么建议吗?非常感谢!
答案 0 :(得分:13)
你必须使用HashMap
吗?
TreeMap
非常适合这类事情。这是一个例子。
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);
答案 1 :(得分:3)
您基本上需要迭代bigMap
中的条目,并决定是否应将其添加到smallMap1
或smallMap2
。
答案 2 :(得分:3)
由于HashMap
是无序的(条目可能以任何顺序排列),因此将其精确分割是没有意义的。我们可以简单地使用交替布尔标志。
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 :(得分:1)
使用bigMap
对for (Entry<Integer, Integer> entry : bigMap.entrySet())
进行迭代,并增加i
以检查是否必须在第一个小地图或第二个小地图中添加该条目。
答案 4 :(得分:1)
以下是SortedMap的解决方案:
var part = location.hostname.split('.');
var subdomains = part.shift();
var upperleveldomains = part.join('.');
答案 5 :(得分:1)
这可能是使用headMap和tailMap的另一种解决方案
SortedMap<Integer, String> map1 = new TreeMap<>();
map1.put(2, "Abc");
map1.put(3, "def");
map1.put(1, "xyz");
map1.put(5, "mddf");
System.out.println(map1);
SortedMap<Integer, String> sm1 = map1.headMap(4); // from 0 to x (key) from front
SortedMap<Integer, String> sm2 = map1.tailMap(1); //tail starting from key
System.out.println("Head Map"+ sm1);
System.out.println("Tail Map"+sm2);
输出为
{1=xyz, 2=Abc, 3=def, 5=mddf}
Head Map{1=xyz, 2=Abc, 3=def}
Tail Map{1=xyz, 2=Abc, 3=def, 5=mddf}
答案 6 :(得分:0)
for (Map.Entry<Integer,Integer> entry : bigMap.entrySet()) {
// ...
}
是迭代原始地图的最快方法。然后,您可以使用Map.Entry键来确定要填充的新地图。
答案 7 :(得分:0)
这是我工作的功能之一,我希望它对其他人有帮助。无论作为键存储的Object / primitive,这都可以工作。
上面提到的TreeMap方法只有在键是基元,有序和索引的精确序列时才有效。
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;
}
答案 8 :(得分:0)
这里有两种简单的分割地图的方法,
/**
*
* @param bulkyMap - your source map to be partitioned
* @param batchSize - partition size
* @return
*/
public List<Map<String, Object>> getMiniMapsInFixedSizeBatches(Map<String, Object> bulkyMap, int batchSize) {
if (batchSize >= bulkyMap.size() || batchSize <= 0) {
return Arrays.asList(bulkyMap);
}
List<Map<String, Object>> batches = new ArrayList<>();
int innerBatchcount = 1;
int count = 1;
Map<String, Object> tempMap = new HashMap<>();
for (Map.Entry<String, Object> entry : bulkyMap.entrySet()) {
tempMap.put(entry.getKey(), entry.getValue());
innerBatchcount++;
count++;
if (innerBatchcount > batchSize || count > bulkyMap.size()) {
innerBatchcount = 1;
Map<String, Object> batchedMap = new HashMap<>();
batchedMap.putAll(tempMap);
batches.add(batchedMap);
tempMap.clear();
}
}
return batches;
}
/**
* the number of partitions is not always guaranteed as the algorithm tries to optimize the number of partitions
* @param bulkyMap - your source map to be partitioned
* @param numPartitions - number of partitions (not guaranteed)
* @return
*/
public List<Map<String, Object>> getMiniPartitionedMaps(Map<String, Object> bulkyMap, int numPartitions) {
int size = bulkyMap.size();
int batchSize = Double.valueOf(Math.ceil(size * 1.0 / numPartitions)).intValue();
return getMiniMapsInFixedSizeBatches(bulkyMap, batchSize);
}
答案 9 :(得分:0)
您可以使用Guava Iterables分区方法和Java流接口来解决它。
import com.google.common.collect.Iterables;
import com.google.common.collect.Lists;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
public static <K, V> List<Map<K, V>> split(Map<K, V> map, int size) {
List<List<Map.Entry<K, V>>> list = Lists.newArrayList(Iterables.partition(map.entrySet(), size));
return list.stream()
.map(entries ->
entries.stream().collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue))
)
.collect(Collectors.toList());
}
答案 10 :(得分:0)
根据@Nizamudeen Karimudeen 的回答构建,如果没有大量重写,我将无法开始工作......此方法适用于任何包含任何类的 HashMap。
那么,假设您要拆分的 Map 是这样定义的:
Map<String, MyClass> myMap = new HashMap<>();
如果你想把它分成 20 个独立的地图,你可以像这样简单地把它分开:
List<Map<String, MyClass>> splitMapList = splitMap(myMap, 20);
然后要使用每个单独的地图,您可以像这样遍历它们:
for (Map<String, MyClass> mySplitMap : splitMapList) {
for(String key : mySplitMap.keySet()) {
MyClass myClass = mySplitMap.get(key);
}
}
或者你可以通过列表的索引等直接引用它们
Map<String, MyClass> subMap = splitMapList.get(3);
方法如下:
public static List<Map<KeyClass, ValueClass>> splitMap(Map<KeyClass, ValueClass> originalMap, int splitSize) {
int mapSize = originalMap.size();
int elementsPerNewMap = mapSize / splitSize;
List<Map<KeyClass, ValueClass>> newListOfMaps = new ArrayList<>(); //Will be returned at the end after it's built in the final loop.
List<List<KeyClass>> listOfMapKeysForIndexing = new ArrayList<>(); //Used as a reference in the final loop.
List<KeyClass> listOfAllKeys = new ArrayList<>(originalMap.keySet());
int maxIndex = listOfAllKeys.size() - 1; //We use this in the first loop to make sure that we never exceed this index number or we will get an index out of range.
int startIndex = 0;
int endIndex = elementsPerNewMap;
for (int i = 0; i < splitSize; i++) { //Each loop creates a new list of keys which will be the entire set for a new subset of maps (total number set by splitSize.
listOfMapKeysForIndexing.add(listOfAllKeys.subList(startIndex, endIndex));
startIndex = Math.min((endIndex + 1), maxIndex);//Start at the next index, but don't ever go past the maxIndex or we get an IndexOutOfRange Exception
endIndex = Math.min((endIndex + elementsPerNewMap), maxIndex);//Same thing for the end index.
}
/*
* This is where we use the listOfMapKeysForIndexing to create each new Map that we add to the final list.
*/
for(List<KeyClass> keyList: listOfMapKeysForIndexing){
Map<KeyClass,ValueClass> subMap = new HashMap<>(); //This should create a quantity of these equal to the splitSize.
for(KeyClass key: keyList){
subMap.put(key,originalMap.get(key));
}
newListOfMaps.add(subMap);
}
return newListOfMaps;
}