嵌套HashMap集合的遍历

本文提供了HashMap集合嵌套HashMap集合的遍历方法
方法如下:
1.新建一个HashMap嵌套集合

package test1;

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

public class MapDemo3 {
public static void main(String[] args) {
	//新建一个嵌套HashMap集合
	Map<String,HashMap<String,String>> map = new HashMap<String,HashMap<String,String>>();
	HashMap<String,String>  hm1 = new HashMap<String,String>();
	HashMap<String,String>  hm2 = new HashMap<String,String>();
	hm1.put("关羽","青龙偃月刀");
	hm1.put("张飞","丈八蛇矛");
	hm2.put("吴用","智多星");
	hm2.put("林冲","豹子头");
	map.put("三国演义", hm1);
	map.put("水浒传", hm2);

2.嵌套HashMap的遍历

1)第一种方法(简单,建议使用)

//嵌套HashMap的遍历
	for(Map.Entry<String,HashMap<String,String>> entry : map.entrySet()) {
		System.out.println(entry.getKey());
		HashMap<String,String> s = entry.getValue();
		for(HashMap.Entry<String,String> entry1 : s.entrySet()) {
			
			System.out.println("\t"+entry1.getKey()+"-----"+entry1.getValue()+"\t");
		}
		

2)第二种方法

System.out.println("-------------------");
	Iterator iter = map.entrySet().iterator();
while(iter.hasNext()) {
	HashMap.Entry<String,HashMap<String,String>> entry = (HashMap.Entry<String,HashMap<String,String>>)iter.next();
	String key = entry.getKey();
	HashMap<String,String> value = entry.getValue();
	System.out.println(key);

	for (HashMap.Entry<String,String>hme : value.entrySet()) {
		
		System.out.println("\t"+hme.getKey()+"-----"+hme.getValue());
		}
}

3.运行结果如下:
运行结果

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值