public class MapTest {
public Map<String,Student> students;
public MapTest() {
super();
this.students = new HashMap<String,Student> ();
}
/*
* 测试添加:先判断学生ID是否被占用,如果未被占用,则输入姓名,并创建新的学生对象,最后添加到students中
*/
public void testPut()
{
Scanner console = new Scanner(System.in);
int i = 0;
while(i < 3)
{
System.out.println("请输入学生ID:");
String ID = console.next();
//判断从键盘输入的ID是否被占用,获取该ID值所对应的value值
Student stu = students.get(ID);
//如果value值为空,创建新的学生对象并添加到students中
if(stu == null)
{
//提示输入学生姓名:
System.out.println("请输入学生姓名:");
String name = console.next();
//创建新的学生对象,将ID-name映射传入
Student newStu = new Student(ID,name);
//在Map中添加以该学生ID为键的映射
students.put(ID, newStu);
System.out.println("已成功添加学生:"+students.get(ID).name);
i++;//一共循环三次,总共添加三个学生信息
}
else
{
System.out.println("您所添加的学生已存在,请重新输入");
continue;
}
}
}
/*
* 测试Map中的remove方法
*
*/
public void testRemove()
{
//从键盘输入要删除的学生ID
System.out.println("请输入要删除的学生ID");
while(true)
{
Scanner console = new Scanner(System.in);
String id = console.next();
Student stu = students.get(id);
if(stu == null)
{
System.out.println("您要删除的学号信息不存在,请重新输入要删除的学生ID");
}
else
{
students.remove(id);
System.out.println("您已经成功删除学生"+stu.name);
break;
}
}
}
/*
* 测试Map中的keySet方法
*/
public void testKeySet()
{
Set<String> ks = students.keySet();
Iterator<String> it = ks.iterator();
while(it.hasNext())
{
String str = it.next();
Student stu = students.get(str);
System.out.println(stu.id+","+stu.name);
}
}
public static void main(String[] args) {
MapTest mt = new MapTest();
mt.testPut();
mt.testKeySet();
mt.testRemove();
mt.testKeySet();
}
}
根据ID修改学生信息:
public class MapTest {
public Map<String,Student> students;
public MapTest() {
super();
this.students = new HashMap<String,Student> ();
}
/*
* 测试添加:先判断学生ID是否被占用,如果未被占用,则输入姓名,并创建新的学生对象,最后添加到students中
*/
public void testPut()
{
Scanner console = new Scanner(System.in);
int i = 0;
while(i < 3)
{
System.out.println("请输入学生ID:");
String ID = console.next();
//判断从键盘输入的ID是否被占用,获取该ID值所对应的value值
Student stu = students.get(ID);
//如果value值为空,创建新的学生对象并添加到students中
if(stu == null)
{
//提示输入学生姓名:
System.out.println("请输入学生姓名:");
String name = console.next();
//创建新的学生对象,将ID-name映射传入
Student newStu = new Student(ID,name);
//在Map中添加以该学生ID为键的映射
students.put(ID, newStu);
System.out.println("已成功添加学生:"+students.get(ID).name);
i++;//一共循环三次,总共添加三个学生信息
}
else
{
System.out.println("您所添加的学生已存在,请重新输入");
continue;
}
}
}
/*
* 测试Map中的remove方法
*
*/
public void testRemove()
{
//从键盘输入要删除的学生ID
System.out.println("请输入要删除的学生ID");
while(true)
{
Scanner console = new Scanner(System.in);
String id = console.next();
Student stu = students.get(id);
if(stu == null)
{
System.out.println("您要删除的学号信息不存在,请重新输入要删除的学生ID");
}
else
{
students.remove(id);
System.out.println("您已经成功删除学生"+stu.name);
break;
}
}
}
/*
* 测试Map中的修改方法
*/
public void testModify()
{
//提示用户要修改的学生信息
System.out.println("请输入要修改的学生ID:");
while(true)
{
Scanner console = new Scanner(System.in);
String id = console.next();
Student stu = students.get(id);
if(stu == null)
{
System.out.println("您要修改的学生信息不存在,请重新输入");
continue;
}
System.out.println("当前学生ID所对应的学生姓名为:"+stu.name);
System.out.println("请输入要修改的学生姓名:");
String name = console.next();
Student newStu = new Student(id,name);
students.put(id, newStu);
System.out.println("修改成功");
break;
}
}
/*
* 测试Map中的keySet方法
*/
public void testKeySet()
{
Set<String> ks = students.keySet();
Iterator<String> it = ks.iterator();
while(it.hasNext())
{
String str = it.next();
Student stu = students.get(str);
System.out.println(stu.id+","+stu.name);
}
}
public static void main(String[] args) {
MapTest mt = new MapTest();
mt.testPut();
mt.testKeySet();
// mt.testRemove();
mt.testModify();
mt.testKeySet();
}
}