Hashtable定义
public class Hashtable<K, V> extends Dictionary<K, V> implements Map<K, V>, Cloneable, Serializable
Hashtable 也是一个散列表,它存储的内容是键值对(key-value)映射。
Hashtable 的函数都是同步的,这意味着它是线程安全的。它的key、value都不可以为null。此外,Hashtable中的映射不是有序的。
Hashtable是线程安全的。
Hashtable的所有API
synchronized void clear()
synchronized Object clone()
boolean contains(Object value)
synchronized boolean containsKey(Object key)
synchronized boolean containsValue(Object value)
synchronized Enumeration<V> elements()
synchronized Set<Entry<K, V>> entrySet()
synchronized boolean equals(Object object)
synchronized V get(Object key)
synchronized int hashCode()
synchronized boolean isEmpty()
synchronized Set<K> keySet()
synchronized Enumeration<K> keys()
synchronized V put(K key, V value)
synchronized void putAll(Map<? extends K, ? extends V> map)
synchronized V remove(Object key)
synchronized int size()
synchronized String toString()
synchronized Collection<V> values()
Hashtable构造函数
public Hashtable() {
this(11, 0.75f);
}
public Hashtable(int initialCapacity) {
this(initialCapacity, 0.75f);
}
public Hashtable(int initialCapacity, float loadFactor) {
if (initialCapacity < 0)
throw new IllegalArgumentException("Illegal Capacity: "+
initialCapacity);
if (loadFactor <= 0 || Float.isNaN(loadFactor))
throw new IllegalArgumentException("Illegal Load: "+loadFactor);
if (initialCapacity==0)
initialCapacity = 1;
this.loadFactor = loadFactor;
table = new Entry<?,?>[initialCapacity];
threshold = (int)Math.min(initialCapacity * loadFactor, MAX_ARRAY_SIZE + 1);
}
public Hashtable(Map<? extends K, ? extends V> t) {
this(Math.max(2*t.size(), 11), 0.75f);
putAll(t);
}
Hashtable遍历:四种
package HashTableDemo;
import java.util.*;
/**
* @ClassName: HashTableTest
* @Author Sandy
* @Date: 2018/12/4
* @Version v1.0.0
* @Description: //TODO
*/
public class HashtableTest {
public static void iteratorHashtableByEntrySet(Hashtable hashtable){
Iterator iterator = hashtable.entrySet().iterator();
while(iterator.hasNext()){
Map.Entry entry = (Map.Entry) iterator.next();
Object key = entry.getKey();
Object value = entry.getValue();
System.out.println("key: "+ key +" value: "+ value);
}
System.out.println();
}
public static void iteratorHashtableByEntryKey(Hashtable hashtable){
Iterator iterator = hashtable.keySet().iterator();
while(iterator.hasNext()){
Object key = iterator.next();
Object value = hashtable.get(key);
System.out.println("key: "+ key +" value: "+ value);
}
System.out.println();
}
public static void iteratorHashtableByValueColl(Hashtable hashtable){
Iterator iterator = hashtable.values().iterator();
while (iterator.hasNext()){
Object value = iterator.next();
System.out.println("value: "+ value);
}
System.out.println();
}
public static void iteratorHashtableByEnumeration(Hashtable hashtable){
Enumeration enumeration = hashtable.elements();
while (enumeration.hasMoreElements()){
System.out.println("element(value): " + enumeration.nextElement());
}
System.out.println();
}
public static void main(String[] args) {
int val;
Object key;
Object value;
Hashtable hashtable = new Hashtable();
Random r = new Random();
for (int i=0; i<5; i++) {
// 随机获取一个[0,100)之间的数字
val = r.nextInt(200);
key = String.valueOf(val);
value = r.nextInt(5);
// 添加到HashMap中
hashtable.put(key, value);
}
iteratorHashtableByEntrySet(hashtable);
iteratorHashtableByEntryKey(hashtable);
iteratorHashtableByValueColl(hashtable);
iteratorHashtableByEnumeration(hashtable);
}
}
输出:
key: 75 value: 1
key: 87 value: 1
key: 2 value: 0
key: 68 value: 2
key: 8 value: 1
key: 75 value: 1
key: 87 value: 1
key: 2 value: 0
key: 68 value: 2
key: 8 value: 1
value: 1
value: 1
value: 0
value: 2
value: 1
element(value): 1
element(value): 1
element(value): 0
element(value): 2
element(value): 1
网友评论