参考文档
我参考了Java 集合系列11之 Hashtable详细介绍(源码解析)和使用示例,阅读了jdk 1.8的源码
Hashtable的继承关系
Hashtable继承了Dictionary类,实现了Map、Cloneable、Serializable接口
public class Hashtable<K,V>
extends Dictionary<K,V>
implements Map<K,V>, Cloneable, java.io.Serializable
Hashtable数据结构
依赖的数组和链表,跟HashMap不一样的
Hashtable是线程安全的
共有方法就是线程安全的方法
Hashtable遍历方法
通过entrySet()去遍历,效率高
通过keySet()去遍历key,效率低
通过values遍历
通过Enumeration keys遍历
通过Enumeration elements遍历
使用例子
import lombok.extern.slf4j.Slf4j;
import java.io.ObjectStreamException;
import java.util.Collection;
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.Map;
import java.util.Random;
import java.util.Set;
@Slf4j
public class HashtableTest {
public static void main(String[] args) {
Hashtable hashtable = new Hashtable();
Random ran = new Random();
for (int i = 0; i < 5; i++) {
int tmp=ran.nextInt(1000);
hashtable.put(Integer.valueOf(i),String.valueOf(tmp));
}
//测试遍历
iteratorEntrySet(hashtable);
iteratorKeySet(hashtable);
iteratorValues(hashtable);
enumerationKeys(hashtable);
enumerationElements(hashtable);
//打印table
log.info("table :{}",hashtable);
//键值对个数
log.info("table size {}",hashtable.size());
//包含键
log.info("contans key 1 :{}",hashtable.containsKey(Integer.valueOf(1)));
//包含values
//remove key
log.info("remove key :{}",hashtable.remove(Integer.valueOf(1)));
log.info("after remove ,table size {}",hashtable.size());
//clear
log.info("clear table ");
hashtable.clear();
//is empety
log.info("is empty: {}", hashtable.isEmpty());
}
//entrySet()去遍历
public static void iteratorEntrySet(Hashtable hashtable) {
Set entry = hashtable.entrySet();
Iterator<Map.Entry> it = entry.iterator();
log.info("iteratorEntrySet:");
while (it.hasNext()) {
Map.Entry entry1=it.next();
log.info("key :{}, value :{}", entry1.getKey(), entry1.getValue());
//下面的逻辑是错误的,it.next()执行了两次,可能为空
//log.info("key :{}, value :{}", it.next().getKey(), it.next().getValue());
}
}
//keySet()去遍历key
public static void iteratorKeySet(Hashtable hashtable){
Set set =hashtable.keySet();
log.info("iteratorKeySet");
Iterator it =set.iterator();
while(it.hasNext()){
Object key =it.next();
log.info("key :{}, value :{}",key,hashtable.get(key));
}
}
//values遍历
public static void iteratorValues(Hashtable hashtable){
Collection values=hashtable.values();
log.info("iteratorValues");
Iterator it =values.iterator();
while(it.hasNext()){
log.info("value :{}",it.next());
}
}
//Enumeration keys遍历
//内部类实现接口Enumeration,通过TYPE实现hasMoreElements() nextElement()方法
public static void enumerationKeys(Hashtable hashtable){
Enumeration keys = hashtable.keys();
log.info("enumerationKeys");
while(keys.hasMoreElements()){
Object key =keys.nextElement();
log.info("key :{}, value :{}",key,hashtable.get(key));
}
}
//Enumeration elements遍历
public static void enumerationElements(Hashtable hashtable){
Enumeration values =hashtable.elements();
log.info("enumerationElements");
while(values.hasMoreElements())
{
Object value =values.nextElement();
log.info("value : {}",value);
}
}
}
运行结果
D:\zxems\work\OpenJdk\8\bin\java.exe "-javaagent:D:\Program Files\JetBrains\IntelliJ IDEA 2019.1.1\lib\idea_rt.jar=52315:D:\Program Files\JetBrains\IntelliJ IDEA 2019.1.1\bin" -Dfile.encoding=GB2312 -classpath D:\zxems\work\OpenJdk\8\jre\lib\charsets.jar;D:\zxems\work\OpenJdk\8\jre\lib\ext\access-bridge-64.jar;D:\zxems\work\OpenJdk\8\jre\lib\ext\cldrdata.jar;D:\zxems\work\OpenJdk\8\jre\lib\ext\dnsns.jar;D:\zxems\work\OpenJdk\8\jre\lib\ext\jaccess.jar;D:\zxems\work\OpenJdk\8\jre\lib\ext\localedata.jar;D:\zxems\work\OpenJdk\8\jre\lib\ext\nashorn.jar;D:\zxems\work\OpenJdk\8\jre\lib\ext\sunec.jar;D:\zxems\work\OpenJdk\8\jre\lib\ext\sunjce_provider.jar;D:\zxems\work\OpenJdk\8\jre\lib\ext\sunmscapi.jar;D:\zxems\work\OpenJdk\8\jre\lib\ext\sunpkcs11.jar;D:\zxems\work\OpenJdk\8\jre\lib\ext\zipfs.jar;D:\zxems\work\OpenJdk\8\jre\lib\jce.jar;D:\zxems\work\OpenJdk\8\jre\lib\jsse.jar;D:\zxems\work\OpenJdk\8\jre\lib\management-agent.jar;D:\zxems\work\OpenJdk\8\jre\lib\resources.jar;D:\zxems\work\OpenJdk\8\jre\lib\rt.jar;D:\My\code\javalearn\java-learn\target\classes;D:\My\tools\maven\repo\org\slf4j\slf4j-log4j12\1.7.2\slf4j-log4j12-1.7.2.jar;D:\My\tools\maven\repo\org\slf4j\slf4j-api\1.7.2\slf4j-api-1.7.2.jar;D:\My\tools\maven\repo\log4j\log4j\1.2.17\log4j-1.2.17.jar java.javabase.map.HashtableTest
2019-07-15 16:57:21,254 [main] INFO java.javabase.map.HashtableTest - iteratorEntrySet:
2019-07-15 16:57:21,254 [main] INFO java.javabase.map.HashtableTest - key :4, value :771
2019-07-15 16:57:21,254 [main] INFO java.javabase.map.HashtableTest - key :3, value :567
2019-07-15 16:57:21,254 [main] INFO java.javabase.map.HashtableTest - key :2, value :933
2019-07-15 16:57:21,254 [main] INFO java.javabase.map.HashtableTest - key :1, value :421
2019-07-15 16:57:21,254 [main] INFO java.javabase.map.HashtableTest - key :0, value :304
2019-07-15 16:57:21,254 [main] INFO java.javabase.map.HashtableTest - iteratorKeySet
2019-07-15 16:57:21,254 [main] INFO java.javabase.map.HashtableTest - key :4, value :771
2019-07-15 16:57:21,254 [main] INFO java.javabase.map.HashtableTest - key :3, value :567
2019-07-15 16:57:21,254 [main] INFO java.javabase.map.HashtableTest - key :2, value :933
2019-07-15 16:57:21,254 [main] INFO java.javabase.map.HashtableTest - key :1, value :421
2019-07-15 16:57:21,254 [main] INFO java.javabase.map.HashtableTest - key :0, value :304
2019-07-15 16:57:21,254 [main] INFO java.javabase.map.HashtableTest - iteratorValues
2019-07-15 16:57:21,254 [main] INFO java.javabase.map.HashtableTest - value :771
2019-07-15 16:57:21,254 [main] INFO java.javabase.map.HashtableTest - value :567
2019-07-15 16:57:21,254 [main] INFO java.javabase.map.HashtableTest - value :933
2019-07-15 16:57:21,254 [main] INFO java.javabase.map.HashtableTest - value :421
2019-07-15 16:57:21,254 [main] INFO java.javabase.map.HashtableTest - value :304
2019-07-15 16:57:21,254 [main] INFO java.javabase.map.HashtableTest - enumerationKeys
2019-07-15 16:57:21,254 [main] INFO java.javabase.map.HashtableTest - key :4, value :771
2019-07-15 16:57:21,254 [main] INFO java.javabase.map.HashtableTest - key :3, value :567
2019-07-15 16:57:21,254 [main] INFO java.javabase.map.HashtableTest - key :2, value :933
2019-07-15 16:57:21,254 [main] INFO java.javabase.map.HashtableTest - key :1, value :421
2019-07-15 16:57:21,254 [main] INFO java.javabase.map.HashtableTest - key :0, value :304
2019-07-15 16:57:21,254 [main] INFO java.javabase.map.HashtableTest - enumerationElements
2019-07-15 16:57:21,254 [main] INFO java.javabase.map.HashtableTest - value : 771
2019-07-15 16:57:21,269 [main] INFO java.javabase.map.HashtableTest - value : 567
2019-07-15 16:57:21,269 [main] INFO java.javabase.map.HashtableTest - value : 933
2019-07-15 16:57:21,269 [main] INFO java.javabase.map.HashtableTest - value : 421
2019-07-15 16:57:21,269 [main] INFO java.javabase.map.HashtableTest - value : 304
2019-07-15 16:57:21,269 [main] INFO java.javabase.map.HashtableTest - table :{4=771, 3=567, 2=933, 1=421, 0=304}
2019-07-15 16:57:21,269 [main] INFO java.javabase.map.HashtableTest - table size 5
2019-07-15 16:57:21,269 [main] INFO java.javabase.map.HashtableTest - contans key 1 :true
2019-07-15 16:57:21,269 [main] INFO java.javabase.map.HashtableTest - remove key :421
2019-07-15 16:57:21,269 [main] INFO java.javabase.map.HashtableTest - after remove ,table size 4
2019-07-15 16:57:21,269 [main] INFO java.javabase.map.HashtableTest - clear table
2019-07-15 16:57:21,269 [main] INFO java.javabase.map.HashtableTest - is empty: true
Process finished with exit code 0