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 studentList1=new ArrayList();
        studentList1.add(new Student("黎明",10));
        studentList1.add(new Student("李俊",11));
        school.add("班级1", studentList1);
        
        List studentList2=new ArrayList();
        studentList2.add(new Student("王涛",9));
        studentList2.add(new Student("王军",11));
        school.add("班级2", studentList2);
        
        //输出每个班级对应的学生信息
        Map> schoolMap=school.getSchoolMap();
        Set>> set=schoolMap.entrySet();
        Iterator>> iter=set.iterator();
        while(iter.hasNext())
        {
            Entry> entry=iter.next();
            String className=entry.getKey();
            System.out.println(className+" 学生信息:");
            List studentList=entry.getValue();
//            for(int i=0;i> schoolMap=new HashMap>();

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

    public String getName() {
        return name;
    }

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

    public Map> getSchoolMap() {
        return schoolMap;
    }

    public void setSchoolMap(Map> 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

你可能感兴趣的:(Java,编程基础)