Java—集合架構Map,java架構map
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為:Tomkey值為:2對應的value為:Jack
請輸入要修改的學生ID
2
當前學生ID,所對應的學生為Jack
請輸入新的學生的姓名:
Lisa
修改成功
總共有2個學生
key值為:1
對應的value為:Tom
key值為:2
對應的value為:Lisa