我想知道是否可以将 HashMap 拆分成更小的子图。
在我的例子中,我有一个包含 100 个元素的 HashMap,我想从原始 HashMap 创建 2 个(或更多)较小的 HashMap,第一个包含从 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>();
有什么建议么?非常感谢!
原文由 RNO 发布,翻译遵循 CC BY-SA 4.0 许可协议
你必须使用
HashMap
吗?TreeMap
对这种事情真的很好。这是一个示例(请注意,0、50 和 99 是映射键, 而不是 索引):