• Java Hashtable遍历与方法使用


    参考文档

    我参考了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:zxemsworkOpenJdk8injava.exe "-javaagent:D:Program FilesJetBrainsIntelliJ IDEA 2019.1.1libidea_rt.jar=52315:D:Program FilesJetBrainsIntelliJ IDEA 2019.1.1in" -Dfile.encoding=GB2312 -classpath D:zxemsworkOpenJdk8jrelibcharsets.jar;D:zxemsworkOpenJdk8jrelibextaccess-bridge-64.jar;D:zxemsworkOpenJdk8jrelibextcldrdata.jar;D:zxemsworkOpenJdk8jrelibextdnsns.jar;D:zxemsworkOpenJdk8jrelibextjaccess.jar;D:zxemsworkOpenJdk8jrelibextlocaledata.jar;D:zxemsworkOpenJdk8jrelibext
    ashorn.jar;D:zxemsworkOpenJdk8jrelibextsunec.jar;D:zxemsworkOpenJdk8jrelibextsunjce_provider.jar;D:zxemsworkOpenJdk8jrelibextsunmscapi.jar;D:zxemsworkOpenJdk8jrelibextsunpkcs11.jar;D:zxemsworkOpenJdk8jrelibextzipfs.jar;D:zxemsworkOpenJdk8jrelibjce.jar;D:zxemsworkOpenJdk8jrelibjsse.jar;D:zxemsworkOpenJdk8jrelibmanagement-agent.jar;D:zxemsworkOpenJdk8jrelib
    esources.jar;D:zxemsworkOpenJdk8jrelib
    t.jar;D:Mycodejavalearnjava-learn	argetclasses;D:My	oolsmaven
    epoorgslf4jslf4j-log4j121.7.2slf4j-log4j12-1.7.2.jar;D:My	oolsmaven
    epoorgslf4jslf4j-api1.7.2slf4j-api-1.7.2.jar;D:My	oolsmaven
    epolog4jlog4j1.2.17log4j-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
    
  • 相关阅读:
    MVC 以内容字节数组形式 实现文件上传 下载
    iis+php+mysql 错误 Call to undefined function mysql_connect()的解决办法
    IIS环境下快速安装、配置和调试PHP5.2.0
    lineheight 在IE6 下失效的问题 解决办法(已经亲手验证)
    基于SOA的体系架构设计
    图片切割系统,这个比较完整了
    MS SQL Server中文版的預設日期datetime格式是yyyymmdd hh:mm:ss.mmm
    “应用程序或DLL C:\WINDOWS\system32\PNCRT.dll为无效的windows映像”的解决方法
    php操作MySQL数据库语句基础
    播放电影有声音没图像
  • 原文地址:https://www.cnblogs.com/JuncaiF/p/11191344.html
Copyright © 2020-2023  润新知