Java-Map测试-学生选课

package collection;

import java.util.*;
import java.util.Map.Entry;

public class MapTest {

    //承装学生类型对象
    public Map<String,Student> students;

    private Scanner console;

    //在构造器中初始化students属性
    public MapTest()
    {
        this.students = new HashMap<String,Student>();
        console = new Scanner(System.in);
    }

    //测试添加 
    public void testPut()
    {
        //输入学生id,判断是否被占用
        Scanner console = new Scanner(System.in);
        int i=0;
        while(i < 3)
        {
            System.out.println("输入学生ID:");
            String ID = console.next();
            Student st = students.get(ID);
            if(st == null)
            {
                //若未被占用 输入姓名创建新学生对象并添加到students中
                System.out.println("请输入学生姓名:");
                String name = console.next();
                Student newStudent = new Student(ID,name);
                students.put(ID, newStudent);
                System.out.println("成功添加学生:"+students.get(ID).name);
                i++;
            }
            else
            {
                System.out.println("该学生ID已被占用。");
                continue;
            }
        }
    }

    //测试Map的keySet方法
    public void testKeySet()
    {
        //通过keySet方法返回所有键的Set集合
        Set<String> keySet = students.keySet();
        //取得students容量
        System.out.println("总共有:"+students.size()+"个学生");
        //遍历keySet取得每一个键 通过get方法得到value
        //Map中的元素也是无序的
        for(String stuId:keySet)
        {
            Student st = students.get(stuId);
            if(st!=null)
            {
                System.out.println("学生"+ st.name);
            }
        }
    }

    //测试删除Map中已有映射
    public void testRemove()
    {

        Scanner console = new Scanner(System.in);
        while(true)
        {
            //提示输入待删除学生ID
            System.out.println("输入要删除的学生ID:");
            String ID = console.next(); 
            //判断此ID是否有对应学生
            Student st = students.get(ID);
            if(st == null)
            {
                System.out.println("输入的ID不存在。");
                continue;
            }
            else
            {
                students.remove(ID);
                System.out.println("成功删除学生:"+st.name);
                break;
            }
        }

    }

    //通过entreSet遍历Map
    public void testEntrySet()
    {
        //返回所有键值对 用entry泛型的Set来接收
        Set<Entry<String,Student>> entrySet = students.entrySet();
        for(Entry<String,Student> entry:entrySet)
        {
            System.out.println("ID:"+entry.getKey());
            System.out.println("姓名:"+entry.getValue().name);
        }
    }

    //put方法修改Map中已有映射
    public void testModify()
    {
        //提示输入要修改学生ID
        System.out.println("输入要修改的学生ID:");
        Scanner console = new Scanner(System.in);
        while(true)
        {
            String stuID = console.next();
            //在students中查找ID对应的学生对象
            Student student = students.get(stuID);
            if(student == null)
            {
                System.out.println("该ID不存在,请重新输入。");
                continue;
            }
            else
            {
                //提示当前对应学生姓名
                System.out.println("当前ID所对应学生姓名为:"+student.name);
                //提示输入新的学生姓名
                System.out.println("请输入新的学生姓名:");
                String name = console.next();
                Student newStudent = new Student(stuID,name);
                students.put(stuID, newStudent);
                System.out.println("修改成功!");
                break;
            }
        }
    }

    //测试Map中是否包含某key值或value值
    //判断学生ID、姓名是否被包含
    public void testContainsKeyOrValue()
    {
        //查询学生ID是否存在
        //在Map中用containsKey()方法来判断是否包含某Key值
        System.out.println("请输入要查询的学生ID:");
        String id = console.next(); 
        System.out.println("您输入的学生ID为:"+id+",在学生映射表中是否存在? "+
                students.containsKey(id));
        if(students.containsKey(id))
        {
            System.out.println("对应的学生为:"+students.get(id).name);
        }

        //查询学生姓名是否存在
        //用containsValue()方法来判断包含某Value值
        System.out.println("请输入要查询的学生姓名:");
        String name = console.next();   
        //得重写学生类的equals和hashCode方法
        if(students.containsValue(new Student(null,name)))
        {
            System.out.println("学生映射表中包含学生:"+name);
        }
        else
        {
            System.out.println("不存在该学生。");
        }

    }

    public static void main(String[] args) {
        MapTest mt = new MapTest();
        mt.testPut();
        mt.testKeySet();
        //mt.testRemove();
        //mt.testEntrySet();
        //mt.testModify();
        //mt.testEntrySet();
        mt.testContainsKeyOrValue();
    }

}
package collection;

import java.util.*;
//学生类
public class Student implements Comparable<Student> {
    public String id;
    public String name;
    public Set<Course> courses;


    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + ((name == null) ? 0 : name.hashCode());
        return result;
    }
    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (!(obj instanceof Student))
            return false;
        Student other = (Student) obj;
        if (name == null) {
            if (other.name != null)
                return false;
        } else if (!name.equals(other.name))
            return false;
        return true;
    }
    public Student(String id,String name)
    {
        this.id = id;
        this.name = name;
        this.courses = new HashSet<Course>();
    }

    //按学生ID排序
    @Override
    public int compareTo(Student o) {
        // 相等返回0 this对象较大返回正值 反之返回负值
        return this.id.compareTo(o.id);
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值