Java中的hashtable() [英] hashtable() in java
问题描述
大家好,这是我很困惑的代码.在打印以下代码的输出时,我看到第10个数字也被打印出来了,这很令人困惑,因为根据Hashtable(2,3),我们只能在哈希对象中放入6个条目,不是吗?如果我错了请向我解释.还请告诉我说负载系数为3,容量为2,它将如何在内部工作?它会创建第一个具有2个条目容量的哈希表,然后如果第三个条目进入,它将创建另一个具有2个条目容量的哈希表,还是通过扩展第3行和第4行来继续现有的哈希表?
导入java.util.*;
公共类哈希{
公共静态void主对象(String args [])引发异常{
Hashtable hash = new Hashtable(2,3); //->阈值为6
for(int i = 0; i< = 10; i ++)
{
整数整数=新的整数(i);
hash.put(integer,"Number:" + i);
}
System.out.println(hash.get(new Integer(5)));
System.out.println(hash.get(new Integer(10)));
}
hi all, here''s the code i''ve confusion in. while printing the output of the following code, i can see number 10 also getting printed which is confusing because as per Hashtable(2,3) , we can only put 6 entries inside hash object, isn''t it ? Explain me if I am wrong. Please also tell me by saying load factor is 3, and capasity is 2, how will it work internally ? will it create first hashtable with 2 entry capasity and then if third entry comes in, it will create another hashtable with 2 entry capasity or will it continue the existing hashtable by expanding it with 3rd and 4th row ?
import java.util.*;
public class hash {
public static void main (String args[]) throws Exception {
Hashtable hash = new Hashtable(2,3); // --> threshold is 6
for (int i = 0; i <= 10; i++)
{
Integer integer = new Integer ( i );
hash.put( integer, "Number : " + i);
}
System.out.println (hash.get(new Integer(5)));
System.out.println (hash.get(new Integer(10)));
}
推荐答案
哈希表将随着新键,值对的插入而增长.您在构造函数中指定的是初始容量和负载因子(用于指定HashTable在增加容量之前的满度).负载系数绝不能高于1.0.您可能根本不需要使用此构造函数.您可以使用:
A hashtable will grow as new key, value pairs are inserted. What you are specifying in the constructor is an initial capacity and a load factor (which specifies how full the HashTable gets before increasing it''s capacity). The load factor should never be higher than 1.0 . You probably don''t need to use this constructor at all. You could just use:
Hashtable hash = new Hashtable();
我对为什么
I am a little confused as to why
hash.get(new Integer(10))
不返回null感到困惑.
isn''t return null.
@jazzy
负载系数(指定HashTable在增加其容量之前的满度)."
例子:
开始时的初始容量为10,负载系数为0.75.如果将10个元素添加到HashTable中,则前7个元素仅计算一个哈希并将该元素添加到bin中.但是,在第8次添加操作中,哈希表将增加其容量.这意味着HashTable将创建一个新的更大的容器(可能是作为数组实现的),并将旧容器的内容复制到新容器中.
@jazzy
" load factor (which specifies how full the HashTable gets before increasing it''s capacity)."
An example:
Start with an initial capacity of 10 and a load factor of 0.75 . If you add 10 elements to the HashTable, the first 7 just compute a hash and add the element to a bin. However, on the 8th add operation, the hashtable will increase its capacity. This means the HashTable will create a new larger container ,which is probably an implemented as an array, and copy the contents of the old container into the new container.
@rick:谢谢迅速回复.但是,您能否举一个小例子来说明我,
是什么意思
@ rick : thanks for you prompt reply. but can you explain me with a small example what do you mean to say by
" load factor (which specifies how full the HashTable gets before increasing it''s capacity)."
这篇关于Java中的hashtable()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!