Map提供了一種映射關系,其中的元素是以鍵值對(key-value)的形式存儲的,key和value可以是任意類型的對象,能夠實現根據key快速查找value。
Map中的鍵值對以Entry類型的對象實例形式存在。
鍵(key值)不可重復,value值可以。
每個鍵最多只能映射到一個值,一個值可以對應多個鍵。
Map接口提供了分別返回key值集合、value值集合以及Entry(鍵值對)集合的方法。Entry類是Map的一個內部類。
Map支持泛型,形式如:Map<Key值類型,V值類型>
HashMap是Map的一個重要實現類,也是最常用的,基於哈希表實現。
HashMap中的Entry對象是無序排列的。
Key值和value值都可以為null,但是一個HashMap只能有一個key值為null的映射
MapTest.java
package com.test.collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Scanner;
import java.util.Set;
public class MapTest {
public Map<String, Student> students;
public MapTest() {
this.students = new HashMap<String, Student>();
}
/**
* 添加
*/
public void testPut(){
Scanner console = new Scanner(System.in);
for (int i = 0; i < 3; i++) {
System.out.println("輸入學生ID:");
String studentId = console.next();
//判斷該ID是否被占用
Student st = students.get(studentId);
if (st == null) {
//提示輸入學生姓名
System.out.println("輸入學生姓名:");
String studentName = console.next();
//創建學生對象
Student newStudent = new Student(studentId, studentName);
//通過調用students的put方法,添加ID-學生映射
students.put(studentId, newStudent);
System.out.println("成功添加學生:" + students.get(studentId).name);
} else {
System.out.println("該學生ID被占用");
continue;
}
}
}
/**
* 通過keySet方法遍歷Map
*/
public void testKeySet() {
//通過keySet方法,返回Map中所有“鍵”的Set集合
Set<String> keySet = students.keySet();
//獲得students的容量
System.out.println("總共有" + students.size() + "個學生");
//遍歷keySet,取得每一個鍵,再調用get方法取得每一個鍵對應的value
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 studentId = console.next();
Student st = students.get(studentId);
if (st == null) {
System.out.println("該學生ID不存在");
continue;
}
students.remove(studentId);
System.out.println("成功刪除學生:" + st.name);
break;
}
}
/**
* 通過entrySet方法遍歷Map
*/
public void testEntrySet() {
Set<Entry<String, Student>> entrySet = students.entrySet();
System.out.println("總共有" + students.size() + "個學生");
for (Entry<String, Student> entry : entrySet) {
System.out.println("key值為:" + entry.getKey());
System.out.println("對應的value為:" + entry.getValue().name);
}
}
/**
* 修改
*/
public void testModify() {
System.out.println("請輸入要修改的學生ID");
Scanner console = new Scanner(System.in);
while (true) {
String studentId = console.next();
Student st = students.get(studentId);
if (st == null) {
System.out.println("該學生ID不存在");
continue;
}
System.out.println("當前學生ID,所對應的學生為" + st.name);
System.out.println("請輸入新的學生的姓名:");
String studentName = console.next();
students.put(studentId, new Student(studentId, studentName));
System.out.println("修改成功");
break;
}
}
public static void main(String[] args) {
MapTest mt = new MapTest();
mt.testPut();
mt.testKeySet();
mt.testRemove();
mt.testEntrySet();
mt.testModify();
mt.testEntrySet();
}
}
執行結果:
輸入學生ID: 1 輸入學生姓名: Tom 成功添加學生:Tom 輸入學生ID: 2 輸入學生姓名: Jack 成功添加學生:Jack 輸入學生ID: 3 輸入學生姓名: Lucy 成功添加學生:Lucy 總共有3個學生 學生:Tom 學生:Jack 學生:Lucy 請輸入要刪除學生的ID: 12 該學生ID不存在 請輸入要刪除學生的ID: 3 成功刪除學生:Lucy
總共有2個學生 key值為:1 對應的value為:Tom key值為:2 對應的value為:Jack
請輸入要修改的學生ID
2
當前學生ID,所對應的學生為Jack
請輸入新的學生的姓名:
Lisa
修改成功
總共有2個學生
key值為:1
對應的value為:Tom
key值為:2
對應的value為:Lisa