增删改查:代码承接connection接口
注意keySet方法和ValueSet方法:遍历输出
Map接口:
*Map提供了一种映射关系,其中的元素是以键值对(key-value)形式存储的,能够实现根据key快速查找value
*Map中的键值对以Entry类型的对象形式存在
*键(key值)不可重复,value值可以
*每个键最多只能映射到一个值,每个value值对应很多key值
*Map接口提供了分别返回key值集合,value值集合以及Entry(键值对)集合的方法
*Map支持泛型,形式如:Map<K,V>
添加方法:用put方法即put(K key, V value>往Map中添加键值对
删除方法:用remove方法remove(object key)如果存在一个键的映射关系,则将其从此映射中移除
修改方法:用put方法,Key值不变,修改value值
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
import java.util.Set;
public class MapTest {
public Map<String,Student> students;
public MapTest() {
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是否被占用,如果key存在与ID相同的值,则返回value值,否则返回null
Student st = students.get(ID);//Map中的get方法
if(st == null) {
System.out.println("请输入学生的姓名:");
String name = console.next();
//创建新的学生对象
Student newStudent = new Student(ID,name);
//通过调用students的put方法,添加ID-学生映射
students.put(ID, newStudent);
System.out.println("成功添加学生:"+students.get(ID).name);
i++;
}else {
System.out.println("该学生已被占用!!!");
continue;
}
}
}
/*
测试删除
*/
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;
}
students.remove(ID);
System.out.println("已删除学生"+st.name);
break;
}
}
/*
测试修改
*/
public void textModify() {
Scanner console = new Scanner(System.in);
while(true) {
System.out.println("请输入修改学生的ID:");
String stuID = console.next();
Student st = students.get(stuID);
if(st == null) {
System.out.println("该ID不存在,请重新输入:");
continue;
}
System.out.println("请输入学生的姓名:");
String name = console.next();
Student newStudent = new Student(stuID, name);
students.put(stuID, newStudent);
System.out.println("修改成功");
break;
}
}
/*
测试Map中的EntrySet方法
*/
public void testEntrySet() {
//通过entrySet方法返回Map中的所有键值对
Set<Map.Entry<String, Student>> entrySet = students.entrySet();
for(Map.Entry<String, Student> entry : entrySet) {
System.out.println("取得键:"+entry.getKey());
System.out.println("对应的值为:"+entry.getValue().name);
}
}
/*
* 测试Map的keySet方法
*/
public void testKeySet(){
//通过KeySet方法,返回Map中的所有"键"的Set集合
Set<String> keySet = students.keySet();
//遍历ketSet,取得每一个键,再调用get方法取得每个键对应的value
for(String stuId : keySet) {
Student st = students.get(stuId);
if(st != null)
System.out.println("学生"+st.name);
}
}
public void testContainsKeyOrValue() {
System.out.println("请输入需要查询的学生ID:");
Scanner console = new Scanner(System.in);
String id = console.next();
//在Map中用containsKey方法来判断是否包含某个Key值
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();
//在Map中用containsValue方法来判断是否包含某个Value值
if(students.containsValue(new Student(null, name)))
System.out.println("在学生映射表中确实存在:"+name);
else
System.out.println("在学生映射表中不存在该学生");
}
public static void main(String[] args) {
// TODO Auto-generated method stub
MapTest mt = new MapTest();
mt.testPut();
mt.testKeySet();
//mt.testRemove();
//mt.testEntrySet();
mt.textModify();
mt.testKeySet();
mt.testContainsKeyOrValue();
}
}
运行结果:
请输入学生的ID:
12
请输入学生的姓名:
海洋
成功添加学生:海洋
请输入学生的ID:
13
请输入学生的姓名:
湖泊
成功添加学生:湖泊
请输入学生的ID:
14
请输入学生的姓名:
维维
成功添加学生:维维
学生海洋
学生湖泊
学生维维
请输入修改学生的ID:
12
请输入学生的姓名:
大海
修改成功
学生大海
学生湖泊
学生维维