HashMap的7种遍历方式

本文详细介绍了HashMap在Java中的7种遍历方法,包括使用迭代器遍历EntrySet和KeySet,使用forEach及lambda表达式,以及通过Streams API进行单线程和多线程遍历。每种方式都提供了具体的代码示例,帮助读者深入理解HashMap的遍历操作。
摘要由CSDN通过智能技术生成

本文总结下HashMap的7种遍历方式。


概述

HashMap遍历大体上可以分为4类:

1,迭代器
2,ForEach 遍历
3,lambda 表达式遍历
4,StreamsApi 遍历

但是每种类型下有不同的实现方式,所以又可以分为7种:

在这里插入图片描述


使用迭代器 EntrySet 的方式遍历

import org.junit.Test;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

public class MyTest {

    @Test
    public void demo1(){
        //创建Map 对象
        Map<Integer, String> map = new HashMap<>();
        //添加数据
        map.put(1,"hello");
        map.put(2,"hello2");
        map.put(3,"hello3");
        map.put(4,"hello4");
        //遍历
        Iterator<Map.Entry<Integer, String>> iterator = map.entrySet().iterator();
        while (iterator.hasNext()){
            Map.Entry<Integer, String> next = iterator.next();
            System.out.println(next.getKey());
            System.out.println(next.getValue());
        }
    }
}

运行结果

在这里插入图片描述


使用迭代器的KeySet

import org.junit.Test;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

public class MyTest {

    @Test
    public void demo1(){
        //创建Map 对象
        Map<Integer, String> map = new HashMap<>();
        //添加数据
        map.put(1,"hello");
        map.put(2,"hello2");
        map.put(3,"hello3");
        map.put(4,"hello4");
        //遍历
        Iterator<Integer> iterator = map.keySet().iterator();
        while (iterator.hasNext()){
            Integer key = iterator.next();
            System.out.print(key + ":");
            System.out.println(map.get(key));
        }
    }
}

程序结果

在这里插入图片描述


使用 For Each EntrySet 的方式进行遍历

import org.junit.Test;

import java.util.HashMap;
import java.util.Map;

public class MyTest {

    @Test
    public void demo1(){
        //创建Map 对象
        Map<Integer, String> map = new HashMap<>();
        //添加数据
        map.put(1,"hello");
        map.put(2,"hello2");
        map.put(3,"hello3");
        map.put(4,"hello4");
        //遍历
        for (Map.Entry<Integer,String> entry: map.entrySet()) {
            
            System.out.println("entry.getKey() = " + entry.getKey());
            System.out.println("entry.getValue() = " + entry.getValue());
        }
    }
}

程序结果

在这里插入图片描述


使用 For Each KeySet 的方式进行遍历

import org.junit.Test;

import java.util.HashMap;
import java.util.Map;

public class MyTest {

    @Test
    public void demo1(){
        //创建Map 对象
        Map<Integer, String> map = new HashMap<>();
        //添加数据
        map.put(1,"hello");
        map.put(2,"hello2");
        map.put(3,"hello3");
        map.put(4,"hello4");
        //遍历
        for (Integer key: map.keySet()) {

            System.out.print(key + ":");
            System.out.println(map.get(key));
        }
    }
}

程序结果

在这里插入图片描述
hr>

使用 Lambda 表达式的方式进行遍历

import org.junit.Test;

import java.util.HashMap;
import java.util.Map;

public class MyTest {

    @Test
    public void demo1(){
        //创建Map 对象
        Map<Integer, String> map = new HashMap<>();
        //添加数据
        map.put(1,"hello");
        map.put(2,"hello2");
        map.put(3,"hello3");
        map.put(4,"hello4");
        //遍历
        map.forEach((key,value) -> {
            System.out.print(key + ":");
            System.out.println(value);
        });
    }
}

程序结果

在这里插入图片描述


使用 Streams API 单线程的方式进行遍历

import org.junit.Test;

import java.util.HashMap;
import java.util.Map;

public class MyTest {

    @Test
    public void demo1(){
        //创建Map 对象
        Map<Integer, String> map = new HashMap<>();
        //添加数据
        map.put(1,"hello");
        map.put(2,"hello2");
        map.put(3,"hello3");
        map.put(4,"hello4");
        //遍历
        map.entrySet().stream().forEach((integerStringEntry -> {

            System.out.print(integerStringEntry.getKey() + ":");
            System.out.println(integerStringEntry.getValue());

        }));
    }
}

程序结果

在这里插入图片描述


使用 Streams API 多线程的方式进行遍历

import org.junit.Test;

import java.util.HashMap;
import java.util.Map;

public class MyTest {

    @Test
    public void demo1(){
        //创建Map 对象
        Map<Integer, String> map = new HashMap<>();
        //添加数据
        map.put(1,"hello");
        map.put(2,"hello2");
        map.put(3,"hello3");
        map.put(4,"hello4");
        //遍历
        map.entrySet().parallelStream().forEach((integerStringEntry -> {

            System.out.println(integerStringEntry.getKey());
            System.out.println(integerStringEntry.getValue());

        }));
    }
}

程序结果

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值