Tag: hashmap

限制Java中HashMap的最大大小

我想限制HashMap的最大大小,以便对我正在实现的各种散列算法采用指标。 我在HashMap的一个重载构造函数中查看了loadfactor。 HashMap(int initialCapacity, float loadFactor) 我尝试在构造函数中将loadFactor设置为0.0f(意味着我不希望HashMap的大小增长为EVER),但javac将此调用为无效: Exception in thread “main” java.lang.IllegalArgumentException: Illegal load factor: 0.0 at java.util.HashMap.(HashMap.java:177) at hashtables.CustomHash.(Main.java:20) at hashtables.Main.main(Main.java:70) Java Result: 1 有没有其他方法来限制HashMap的大小,所以它不会增长?

ArrayList作为HashMap中的键

是否可以添加ArrayList作为HashMap的键。 我想保留双字母的频率数。 二元组是关键,价值在于它的频率。 对于像“他是”这样的每个bigrams,我为它创建一个ArrayList并将其插入到HashMap 。 但我没有得到正确的输出。 public HashMap<ArrayList, Integer> getBigramMap(String word1, String word2) { HashMap<ArrayList, Integer> hm = new HashMap<ArrayList, Integer>(); ArrayList arrList1 = new ArrayList(); arrList1 = getBigram(word1, word2); if (hm.get(arrList1) != null) { hm.put(arrList1, hm.get(arrList1) + 1); } else { hm.put(arrList1, 1); } System.out.println(hm.get(arrList1)); return hm; } public ArrayList getBigram(String word1, String word2) […]

如何迭代的地图?

我有一个Map (实际上我正在使用更复杂的POJO,但为了我的问题而简化它) Person看起来像: class Person { String name; Integer age; //accessors } 如何遍历此地图,打印出密钥,然后是人名,然后是人员年龄,例如: System.out.println(String.format(“Key : %s Name : %s Age : %s”, a, b, c)); A是Map < String ,Person>的关键 B是Person.getName()的名字 C是来自Person.getAge()的年龄 我可以使用.values()从地图中提取所有值,详见HashMap文档 ,但我有点不确定如何获取密钥

创建arraylist的hashmap的最佳方法

我有.txt格式的一百万行数据。 格式很简单。 对于每一行: USER1,值1 用户2,值2 用户3,值3 USER1,VALUE4 … 你知道我的意思。 对于每个用户,它可能会出现多次,或只出现一次(您永远不会知道)。 我需要找出每个用户的所有值。 因为用户可能会随机出现,所以我使用Hashmap来做。 即:HashMap(key:String,value:ArrayList)。 但是要向arrayList添加数据,我必须经常使用HashMap get(key)来获取arrayList,为它添加值,然后将其放回HashMap。 我觉得效率不高。 谁知道更好的方法呢?

“无法创建通用数组…” – 如何创建Map 数组?

我想使用simpleJdbcInsert类和executeBatch方法 public int[] executeBatch(Map[] batch) http://static.springsource.org/spring/docs/2.5.x/api/org/springframework/jdbc/core/simple/SimpleJdbcInsert.html 所以我需要传递一个Map数组作为参数。 如何创建这样的数组? 我试过的是 Map[] myArray = new HashMap[10] 它是错误的:无法创建Map通用数组 List<Map>会更容易,但我想我需要一个数组。 那么如何创建Map数组呢? 谢谢

可以将java数组用作HashMap键

如果HashMap的键是一个字符串数组: HashMap pathMap; 您可以使用新创建的字符串数组访问地图,还是必须是相同的String []对象? pathMap = new HashMap(new String[] { “korey”, “docs” }, “/home/korey/docs”); String path = pathMap.get(new String[] { “korey”, “docs” });