Java 集合 之 泛型实例

http://www.verejava.com/?id=17159819345737

/** 
     * 题目: 
    用集合框架泛型实现 : 
    1. 学校有很多班级,每个班级有多个学生 
    2. 输出每个班级对应的学生信息 

     */ 

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

public class Test {

   
    public static void main(String[] args) {
        School school=new School("海淀实验小学");
        //添加班级 和 对应的学生
        List<Student> studentList1=new ArrayList<Student>();
        studentList1.add(new Student("黎明",10));
        studentList1.add(new Student("李俊",11));
        school.add("班级1", studentList1);
        
        List<Student> studentList2=new ArrayList<Student>();
        studentList2.add(new Student("王涛",9));
        studentList2.add(new Student("王军",11));
        school.add("班级2", studentList2);
        
        //输出每个班级对应的学生信息
        Map<String,List<Student>> schoolMap=school.getSchoolMap();
        Set<Entry<String,List<Student>>> set=schoolMap.entrySet();
        Iterator<Entry<String,List<Student>>> iter=set.iterator();
        while(iter.hasNext())
        {
            Entry<String,List<Student>> entry=iter.next();
            String className=entry.getKey();
            System.out.println(className+" 学生信息:");
            List<Student> studentList=entry.getValue();
//            for(int i=0;i<studentList.size();i++)
//            {
//                Student student=studentList.get(i);
//                System.out.println(student.getName()+","+student.getAge());
//            }
            for(Student student:studentList)
            {
                System.out.println(student.getName()+","+student.getAge());
            }
        }
    }
}



//学校类
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class School {
    private String name;
    private Map<String,List<Student>> schoolMap=new HashMap<String,List<Student>>();

    public School(String name) {
        this.name = name;
    }
    
    //添加班级 和 对应的学生集合
    public void add(String className,List<Student> studentList)
    {
        schoolMap.put(className, studentList);
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Map<String, List<Student>> getSchoolMap() {
        return schoolMap;
    }

    public void setSchoolMap(Map<String, List<Student>> schoolMap) {
        this.schoolMap = schoolMap;
    }
    
    
}



//学生类
public class Student {
    private String name;
    private int age;

    public Student(String name, int age) {
        this.name = name;
        this.age = age;
    }

    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;
    }
    
    
}

http://www.verejava.com/?id=17159819345737

posted @ 2018-06-24 07:52  verejava  阅读(206)  评论(0编辑  收藏  举报