java中HashMap嵌套HashMap嵌套ArrayList案例

来源:互联网 发布:java正则表达式是什么 编辑:程序博客网 时间:2024/06/05 11:56

基础学生类

package itcast06;public class Student {    // 学生姓名    private String name;    // 学生年龄    private int age;    // 无参构造    public Student() {        super();    }    // 带参构造    public Student(String name, int age) {        super();        this.name = name;        this.age = age;    }    // getXxx() setXxx()方法    public String getName() {        return name;    }    public void setName(String name) {        this.name = name;    }    public int getAge() {        return age;    }    public void setAge(int age) {        this.age = age;    }}

实现嵌套类

package itcast06;import java.util.ArrayList;import java.util.HashMap;import java.util.Set;/** * HashMap嵌套HashMap嵌套ArrayList * 示例: * 中国 china *  *      安徽省ah *          安徽大学ahu *              法学      39 *              计算机 76 *          合肥工业大学hfut *              电子      12 *              物理      69 *  *      山东省sd *          山东大学sdu *              法学      73 *              计算机 99 *          济南大学 ujn *              电子      55 *              物理      27 *  *      江苏省js *          南京大学nju *              法学      36 *              计算机 82 *          东南大学seu *              电子      61 *              物理      30 *  * @author lgt * */public class HashMapDemo {    public static void main(String[] args) {        // 创建大集合,使用HashMap,其中HashMap无序,而LinkedHashMap具有可以预知的迭代顺序        HashMap<String, HashMap<String, ArrayList<Student>>> chMap = new HashMap<String, HashMap<String, ArrayList<Student>>>();        // 安徽省ah数据        // 创建省区集合        HashMap<String, ArrayList<Student>> ahMap = new HashMap<String, ArrayList<Student>>();        // 创建安徽大学学生集合        ArrayList<Student> array1 = new ArrayList<Student>();        // 创建安徽大学学生元素        Student s1 = new Student("法学", 39);        Student s2 = new Student("计算机", 76);        // 添加学生元素到学生集合        array1.add(s1);        array1.add(s2);        // 创建合肥工业大学学生集合        ArrayList<Student> array2 = new ArrayList<Student>();        // 创建合肥工业大学学生元素        Student s3 = new Student("电子", 12);        Student s4 = new Student("物理", 69);        // 添加学生元素到学生集合        array2.add(s3);        array2.add(s4);        // 添加学校学生键值对到省区集合        ahMap.put("安徽大学", array1);        ahMap.put("合肥工业大学", array2);        // 添加省区学校键值对到国家集合        chMap.put("安徽省", ahMap);        // 山东省sd数据        HashMap<String, ArrayList<Student>> sdMap = new HashMap<String, ArrayList<Student>>();        // 创建山东大学学生集合        ArrayList<Student> array3 = new ArrayList<Student>();        // 创建山东大学学生元素        Student s5 = new Student("法学", 73);        Student s6 = new Student("计算机", 99);        // 添加学生元素到学生集合        array3.add(s5);        array3.add(s6);        // 创建济南大学学生集合        ArrayList<Student> array4 = new ArrayList<Student>();        // 创建济南大学学生元素        Student s7 = new Student("电子", 55);        Student s8 = new Student("物理", 27);        // 添加学生元素到学生集合        array4.add(s7);        array4.add(s8);        // 添加学校学生键值对到省区集合        sdMap.put("山东大学", array3);        sdMap.put("济南大学", array4);        // 添加省区学校键值对到国家集合        chMap.put("山东省", sdMap);        // 江苏省js数据        HashMap<String, ArrayList<Student>> jsMap = new HashMap<String, ArrayList<Student>>();        // 创建南京大学学生集合        ArrayList<Student> array5 = new ArrayList<Student>();        // 创建南京大学学生元素        Student s9 = new Student("法学", 36);        Student s10 = new Student("计算机", 82);        // 添加元素到学生集合        array5.add(s9);        array5.add(s10);        // 创建东南大学学生集合        ArrayList<Student> array6 = new ArrayList<Student>();        // 创建东南大学学生元素        Student s11 = new Student("电子", 61);        Student s12 = new Student("物理", 30);        // 添加元素到学生集合        array6.add(s11);        array6.add(s12);        // 添加学校学生键值对到省区集合        jsMap.put("南京大学", array5);        jsMap.put("东南大学", array6);        // 添加省区学校键值对到国家集合        chMap.put("江苏省", jsMap);        // 遍历集合        System.out.println("中国");        // 获取所有的键        Set<String> chMapSet = chMap.keySet();        // 遍历键的集合,获取每一个键        for (String chMapKey : chMapSet) {            System.out.println("\t" + chMapKey);            // 根据键查找值            HashMap<String, ArrayList<Student>> chMapValue = chMap.get(chMapKey);            // 获取所有的键            Set<String> chMapValueSet = chMapValue.keySet();            // 遍历键的集合,获取每一个键            for (String chMapValueKey : chMapValueSet) {                // 根据键查找值                System.out.println("\t\t" + chMapValueKey);                ArrayList<Student> chMapValueValue = chMapValue.get(chMapValueKey);                for (Student s : chMapValueValue) {                    System.out.println("\t\t\t" + s.getName() + "---" + s.getAge());                }            }        }    }}
0 0
原创粉丝点击