java

关注公众号 jb51net

关闭
首页 > 软件编程 > java > HashMap的6种遍历方式

Java中HashMap集合的6种遍历方式详解

作者:warybee

这篇文章主要介绍了Java中HashMap集合的6种遍历方式详解,HashMap 基于哈希表的 Map 接口实现,是以 key-value 存储形式存在,即主要用来存放键值对,HashMap 的实现不是同步的,这意味着它不是线程安全的,我们来看一下其遍历方式,需要的朋友可以参考下

HashMap的6种遍历方式

1 Foreach entrySet方式

     @Test
     public void test1(){
         HashMap<Integer,String> languages=new HashMap<>();
         languages.put(1,"java");
         languages.put(2,"C#");
         languages.put(3,"javascript");
         languages.put(4,"C");
         languages.put(5,"C++");
         for (Map.Entry<Integer,String> entry:languages.entrySet()) {
             log.info("key:"+entry.getKey());
             log.info("value:"+entry.getValue());
         }
     }

2 Foreach keySet方式

@Test
public void test2(){
    HashMap<Integer,String> languages=new HashMap<>();
    languages.put(1,"java");
    languages.put(2,"C#");
    languages.put(3,"javascript");
    languages.put(4,"C");
    languages.put(5,"C++");
    for (Integer key:languages.keySet()) {
        log.info("key:"+key);
        log.info("value:"+languages.get(key));
    }
}

3 Iterator EntrySet方式

@Test
public void test3(){
    HashMap<Integer,String> languages=new HashMap<>();
    languages.put(1,"java");
    languages.put(2,"C#");
    languages.put(3,"javascript");
    languages.put(4,"C");
    languages.put(5,"C++");
    Iterator<Map.Entry<Integer, String>> iterator = languages.entrySet().iterator();
    while (iterator.hasNext()){
        Map.Entry<Integer, String> next = iterator.next();
        log.info("key:"+next.getKey());
        log.info("value:"+next.getValue());
    }
}

4 Iterator KeySet方式

@Test
public void test4(){
    HashMap<Integer,String> languages=new HashMap<>();
    languages.put(1,"java");
    languages.put(2,"C#");
    languages.put(3,"javascript");
    languages.put(4,"C");
    languages.put(5,"C++");
    Iterator<Integer> iterator = languages.keySet().iterator();
    while (iterator.hasNext()){
        log.info("key:"+iterator.next());
        log.info("value:"+languages.get(iterator.next()));
    }
}

5 Lambda表达式

   @Test
    public void test5(){
        HashMap<Integer,String> languages=new HashMap<>();
        languages.put(1,"java");
        languages.put(2,"C#");
        languages.put(3,"javascript");
        languages.put(4,"C");
        languages.put(5,"C++");
        languages.forEach((k,v)->{
            log.info("key:"+k);
            log.info("value:"+v);
        });
    }

6 Stream API方式

Stream API 单线程

    @Test
    public void test6(){
        HashMap<Integer,String> languages=new HashMap<>();
        languages.put(1,"java");
        languages.put(2,"C#");
        languages.put(3,"javascript");
        languages.put(4,"C");
        languages.put(5,"C++");
        languages.entrySet().stream().forEach(entry->{
            log.info("key:"+entry.getKey());
            log.info("value:"+entry.getValue());
        });
    }

Stream API 多线程

@Test
    public void test7(){
        HashMap<Integer,String> languages=new HashMap<>();
        languages.put(1,"java");
        languages.put(2,"C#");
        languages.put(3,"javascript");
        languages.put(4,"C");
        languages.put(5,"C++");
        languages.entrySet().parallelStream().forEach(entry->{
            log.info("key:"+entry.getKey());
            log.info("value:"+entry.getValue());
        });
    }

到此这篇关于Java中HashMap集合的6种遍历方式详解的文章就介绍到这了,更多相关HashMap的6种遍历方式内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

您可能感兴趣的文章:
阅读全文