zoukankan      html  css  js  c++  java
  • 集合嵌套

    看了学习视频,来一段代码

    import java.util.HashMap;
    import java.util.Iterator;
    import java.util.Map;
    import java.util.Set;
    public class jiheqiantao {
        public static void main(String[] args) {
            HashMap<String, String> javase = new HashMap<String, String>(); // 基础班
            HashMap<String, String> javaee = new HashMap<String, String>(); // 就业班
            
            javase.put("001", "张三");
            javase.put("002", "李四");
            
            javaee.put("001", "王五");
            javaee.put("002", "赵六");
            
            HashMap<String, HashMap<String, String>> czbk = 
                    new HashMap<String, HashMap<String, String>>();
            czbk.put("基础班", javase); // 基础班对应一个HashMap集合
            czbk.put("就业班", javaee); // 就业班对应一个HashMap集合
            keySet(czbk);
            entrySet(czbk);
        }
        // 利用entrySet遍历,效率比keySet要高
        public static void entrySet(HashMap<String, HashMap<String, String>> czbk) {
            Set<Map.Entry<String, HashMap<String, String>>> classNameSet = 
                    czbk.entrySet();
            Iterator<Map.Entry<String, HashMap<String, String>>> classNameIt = 
                    classNameSet.iterator();
            while (classNameIt.hasNext()) {
                Map.Entry<String, HashMap<String, String>> classNameEntry = 
                        classNameIt.next();
                String classNameKey = classNameEntry.getKey();
                HashMap<String, String> classMap = classNameEntry.getValue();
                Set<Map.Entry<String, String>> studentSet = classMap.entrySet();
                Iterator<Map.Entry<String, String>> studentIt = studentSet.iterator();
                
                
                while (studentIt.hasNext()) {
                    Map.Entry<String, String> studentEntry =
                            studentIt.next();
                    String numKey = studentEntry.getKey();
                    String nameValue = studentEntry.getValue();
                    System.out.println(classNameKey + "..." + numKey + "..." + nameValue);
                }
            }
        }
        
        
        
        public static void keySet(HashMap<String, HashMap<String, String>> czbk) {
            Set<String> classNameSet = czbk.keySet();
            Iterator<String> classNameIt = classNameSet.iterator();
            while (classNameIt.hasNext()) {
                String classNameKey = classNameIt.next();
                HashMap<String, String> classMap = czbk.get(classNameKey);
                Set<String> studentNum = classMap.keySet();
                Iterator<String> studentIt = studentNum.iterator();
                while (studentIt.hasNext()) {
                    String numKey = studentIt.next();
                    String nameValue = classMap.get(numKey);
                    System.out.println(classNameKey + "..." + numKey + "..." + nameValue);
                }
            }
        }
    }
    
    ========================================Talk is cheap, show me the code=======================================
    CSDN博客地址:https://blog.csdn.net/qq_34115899
  • 相关阅读:
    数据库性能测试---前阿里数据库团队资深DBA杨奇龙
    阿里云 MYSQL 与 PG(丁奇与德哥)
    RHEL6中ulimit的nproc限制
    Linux下文件描述符
    Linux中的文件描述符与打开文件之间的关系------------每天进步一点点系列
    5.6 太多分区引起OOM
    Linux lsof命令使用小结
    重现PHP Core的调用栈
    用GDB排查Python程序故障
    mysql 索引优化
  • 原文地址:https://www.cnblogs.com/lcy0515/p/9179842.html
Copyright © 2011-2022 走看看