遍历Map的2种方法

import java.util.*;

import cn.tianliangedu.jihe.Student2;

public class BianliMap {

    public static void main(String[] args) {
        test();
    }
    public static void test()
    {
        Map<String, Student2> map2 = new HashMap<String, Student2>();
        map2.put("小明", new Student2("小明",22));
        map2.put("小红", new Student2("小红",98));
        map2.put("小杨", new Student2("小杨",95));
        System.out.println(map2.get("小明"));
        
        //第一种遍历map的方法,拿到KeySet,遍历keySet
        Set<String> keySet = map2.keySet();
        for(String arr:keySet)
        {
            System.out.print("键="+arr+"\t");
            System.out.println("值="+map2.get(arr));
        }
        System.out.println("===================");
        
        //第二种遍历map的方法,拿到entrySet,遍历entrySet(entrySet是键值对)
        Set<Map.Entry<String,Student2>> entrySet = map2.entrySet();
        for(Map.Entry<String, Student2> es:entrySet)
        {
            System.out.print("键="+es.getKey()+"\t");
            System.out.println("值="+es.getValue());
        }
    }
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值