Java基础教程之Map遍历的5种方式

 更新时间:2024年01月28日 11:30:13   作者:多多钟意你吖  
Map作为Java中的一种集合,以键值对的形式存放一批数据,经常会被我们应用在项目中,这篇文章主要给大家介绍了关于Java基础教程之Map遍历的5种方式,需要的朋友可以参考下

前言

首先我们需要把map转换为set进行遍历,可使用entrySet和keySet共2种方式进行转换。

每一种情况都可以采用下面的方式进行遍历:

分别是使用迭代器iterator()遍历;增强for循环遍历;forEach+lambda循环遍历,将循环简化;还可以直接通过方法获取Collection集合在进行便利;最后一个就是使用streams流遍历。

创建一个集合

    HashMap<Integer, String> map = new HashMap<>();
        map.put(1,"ljj");
        map.put(2,"zsy");
        map.put(3,"sxf");
        map.put(4,"wjx");
    }

方式一:Iterator 迭代器遍历

map.entrySet().iterator();

map.keySet().iterator();

//Iterator 迭代器遍历
    public static void iterator(Map<Integer, String> map){
        System.out.println("---使用entrySet()迭代器进行遍历map集合---");
        Iterator<Map.Entry<Integer, String>> entryIterator = map.entrySet().iterator();
        while (entryIterator.hasNext()){
            Map.Entry<Integer, String> entry = entryIterator.next();
            System.out.println(entry.getKey()+":"+entry.getValue());
        }
        System.out.println("---使用KeySet()迭代器进行遍历map集合---");
        Iterator<Integer> keySetIterator = map.keySet().iterator();
        while (keySetIterator.hasNext()){
            Integer key = keySetIterator.next();
            System.out.println(key+":"+map.get(key));
        }

    }

方式二:For Each方式遍历

map.forEach(BiConsumer action)

 //For Each方式遍历
    public static void forEach_map(Map<Integer,String> map){
        System.out.println("map.forEach(BiConsumer action)方法遍历map");
        map.forEach((key,value) -> System.out.println(key+" : "+value));
        
    }

方式三:获取Collection集合

map.values().forEach()

map中的values()方法,通过这个方法可以直接获取Map中存储所有值的Collection集合

 //values()方法
    public static void Collection_forEach(Map<Integer,String> map){
        System.out.println("获取map集合的values值集合对象,进行forEach遍历");
        //Map中的values()方法,通过这个方法可以直接获取Map中存储所有值的Collection集合
        Collection<String> values = map.values();
        values.forEach( v -> System.out.println(v));
    }

方式四:增强for遍历map

map.entrySet().for

map.keySet().for

    public static void for_plus(Map<Integer,String> map){
        System.out.println("增强for循环,Map.Entry<>");
        for (Map.Entry<Integer,String> entry : map.entrySet()){
            String value = entry.getValue();
            System.out.println(value);
        }
        System.out.println("增强for循环,map.keySet");
        for (Integer key : map.keySet()){
            String value = map.get(key);
            System.out.println(value);
        }
    }

方法五:Stream流遍历

map.entrySet().stream().forEach()

map.keySet().stream().forEach()

//Stream流遍历
    public static void stream_list(Map<Integer,String> map){
        System.out.println("map.entrySet().stream.forEach()遍历—Stream流遍历");
        map.entrySet().stream().forEach((Map.Entry<Integer,String> entry) -> {
            System.out.print(entry.getKey()+":");
            System.out.println(entry.getValue());
        });
        System.out.println("map.keySet().stream.forEach()遍历—Stream流遍历");
        map.keySet().stream().forEach(key -> {
            System.out.println(map.get(key));
        });
    }

附:遍历Map的性能比较

不同的遍历方式对Map的性能有影响,下面通过实验来看看它们之间的性能差异。

实验代码如下:

Map map = new HashMap<>();
Random random = new Random();
for (int i = 0; i < 1000000; i++) {
    map.put(random.nextInt(1000000), "some value");
}

long start = System.currentTimeMillis();
for (Map.Entry entry : map.entrySet()) {
    // do nothing
}
long end = System.currentTimeMillis();
System.out.println("遍历entrySet耗时:" + (end - start) + "ms");

start = System.currentTimeMillis();
for (Integer key : map.keySet()) {
    // do nothing
}
end = System.currentTimeMillis();
System.out.println("遍历keySet耗时:" + (end - start) + "ms");

start = System.currentTimeMillis();
for (String value : map.values()) {
    // do nothing
}
end = System.currentTimeMillis();
System.out.println("遍历values耗时:" + (end - start) + "ms");

实验结果(遍历1000000个元素,单位:毫秒):

遍历entrySet耗时:14ms

遍历keySet耗时:16ms

遍历values耗时:33ms

从实验结果来看,通过entrySet遍历Map是最快的,而通过values遍历是最慢的。

写到最后

到此这篇关于Java基础教程之Map遍历的5种方式的文章就介绍到这了,更多相关Java Map遍历方式内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

最新评论