package collection;
import java.util.*;
import java.util.Map.Entry;
public class MapTest {
public Map<String,Student> students;
private Scanner console;
public MapTest()
{
this.students = new HashMap<String,Student>();
console = new Scanner(System.in);
}
public void testPut()
{
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)
{
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;
}
}
}
public void testKeySet()
{
Set<String> keySet = students.keySet();
System.out.println("总共有:"+students.size()+"个学生");
for(String stuId:keySet)
{
Student st = students.get(stuId);
if(st!=null)
{
System.out.println("学生"+ st.name);
}
}
}
public void testRemove()
{
Scanner console = new Scanner(System.in);
while(true)
{
System.out.println("输入要删除的学生ID:");
String ID = console.next();
Student st = students.get(ID);
if(st == null)
{
System.out.println("输入的ID不存在。");
continue;
}
else
{
students.remove(ID);
System.out.println("成功删除学生:"+st.name);
break;
}
}
}
public void testEntrySet()
{
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);
}
}
public void testModify()
{
System.out.println("输入要修改的学生ID:");
Scanner console = new Scanner(System.in);
while(true)
{
String stuID = console.next();
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;
}
}
}
public void testContainsKeyOrValue()
{
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);
}
System.out.println("请输入要查询的学生姓名:");
String name = console.next();
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.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>();
}
@Override
public int compareTo(Student o) {
return this.id.compareTo(o.id);
}
}