zoukankan      html  css  js  c++  java
  • Java的map键值对的用法,map的遍历,Entry对象的使用

    思路:

    1、定义集合

    2、存储数据

    3、添加元素

    4、遍历

      4.1将需要遍历的集合的键封装到set集合中(这用到了entrySet方法,和Entry对象)

      4.2声明迭代器或者用for增强循环

      4.3通过set集合的getKey和getValue方法拿到值和键

    5、打印输出就好

    代码呈现如下:

     1 package com.aaa.demo;
     2 
     3 import java.util.HashMap;
     4 import java.util.Iterator;
     5 import java.util.Map.Entry;
     6 import java.util.Set;
     7 
     8 public class MaoMapDemo {
     9     public static void main(String[] args) {
    10         //定义Java班的集合
    11         HashMap<String, String> javas=new HashMap<String,String>();
    12         //定义Hdoop班的集合
    13         HashMap<String, String> Hdoop=new HashMap<String,String>();
    14         //向班级存储学生
    15         javas.put("001", "大冰");
    16         javas.put("002", "北岛");
    17         
    18         Hdoop.put("001", "舒婷");
    19         Hdoop.put("002", "食指");
    20         //定义aaa容器,键是班级的名字 值是两个班级的容器
    21         HashMap<String, HashMap<String, String>> aaa=new HashMap<String,HashMap<String,String>>();
    22         aaa.put("现代", javas);
    23         aaa.put("近代", Hdoop);
    24         entrySet1(aaa);
    25     }
    26     public static void entrySet(HashMap<String,HashMap<String,String>> aaa){
    27         //调用集合aaa的方法  entrySet将aaa集合的键封装到Set集合中
    28         Set<Entry<String,HashMap<String,String>>> classNameSet=aaa.entrySet();
    29         //迭代Set集合
    30         Iterator<Entry<String,HashMap<String,String>>> it=classNameSet.iterator();
    31         while(it.hasNext()){
    32             Entry<String,HashMap<String,String>> next=it.next();
    33             //getKey getValue 得到键和值
    34             String classNameKey = next.getKey();
    35             System.out.println(classNameKey);
    36             //aaa容器的键  班级姓名classMap
    37             HashMap<String,String> classMap=next.getValue();
    38             //entrySet将classMap集合的键封装到set集合中
    39             Set<Entry<String,String>> studentSet=classMap.entrySet();
    40             //迭代
    41             Iterator<Entry<String, String>> studentIt = studentSet.iterator();
    42             while(studentIt.hasNext()){
    43                 //遍历集合
    44                 Entry<String,String> studentEntry=studentIt.next();
    45                 String numKey = studentEntry.getKey();
    46                 String nameValue = studentEntry.getValue();
    47                 System.out.println(numKey+":    "+nameValue);
    48             }
    49         }
    50     }
    51     public static void entrySet1(HashMap<String,HashMap<String,String>> aaa){
    52         //调用集合aaa的方法  entrySet将aaa集合的键封装到Set集合中
    53         Set<Entry<String,HashMap<String,String>>> cNS=aaa.entrySet();
    54         //for循环遍历得到班级
    55         for(Entry<String,HashMap<String,String>> next:cNS){
    56             //getKey getValue 得到键和值
    57             String classKey = next.getKey();
    58             //aaa容器的键  班级姓名classMap
    59             HashMap<String, String> classValue = next.getValue();
    60             //entrySet将classMap集合的键封装到set集合中
    61             Set<Entry<String, String>> studentSet = classValue.entrySet();
    62             System.out.println(classKey);
    63             //for循环遍历
    64             //Set<Entry<String, String>> entrySet = classValue.entrySet();
    65             for(Entry<String, String> studentEntry:studentSet){
    66                    String numKey = studentEntry.getKey();
    67                    String nameValue = studentEntry.getValue();
    68                    System.out.println(numKey+":   "+nameValue);
    69             }
    70         }
    71     }
    72 }
  • 相关阅读:
    Solution SQL Server 2008 Configuration Manager Cannot open problem.
    Grant, Revoke, Deny
    Solution: 无法察看SQL Server 数据库属性 错误: &quot;There is no row at position 0. (System.Data)&quot;
    SQL SMO 学习
    恢复误删的桌面 回收站
    SQL Server Security Understanding.
    SQL Server 中的 Schema, User, Login, Role
    形而上学 辩证法
    文档归档的配置发送
    无法从服务器加载工作流操作
  • 原文地址:https://www.cnblogs.com/yanpingping/p/10581513.html
Copyright © 2011-2022 走看看