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
  • 相关阅读:
    郁闷,母版页为什么会这样?怎么在使用了母版页的情况下使用js?大家帮忙
    .NET中实现无刷新客户端联动下拉菜单 (无刷新)(一)
    ADO.NET(二)
    HasRows的返回值问题
    动态生成DataTable绑定至DataList一例
    关于FastReport4.3的使用心得1
    资源文件的编译
    加密当前数据库的所有存储过程。
    使用拼音首字母序列实现检索功能
    关于错误Access Violation和too many consecutive exceptions,解决方法
  • 原文地址:https://www.cnblogs.com/lcy0515/p/9179842.html
Copyright © 2011-2022 走看看