標籤:port 技術分享 import ges src 索引值 jpg 返回 pac
Map介面
1.Map提供了一種映射關係,其中的元素是以索引值對(key-value)的形式儲存的,能夠實現根據key快速尋找value
2.Map中的索引值對以Entry類型的對象執行個體形式存在
3.鍵(key值)不可重複,value可以
4.每個鍵最多隻能映射到一個值
5.Map介面提供了分別返回key值集合,value值集合以及Entry(索引值對)集合的方法
6.Map支援泛型,形式如:Map<K,V>
HashMap類
1.HashMap是Map 的一個重要實作類別,也是最常用的,基於雜湊表實現
2.HashMap中的Entry對象是無序排列的
3.key值和value值都可以為null,但是一個HashMap只能有一個key值為null的映射(key值不可重複)
- 學生選課——使用Map新增學生
- 學生選課——刪除Map中的學生
- 學生選課——修改Map中的學生
package com.imooc.collection;import java.util.HashMap;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; /*在構造器中初始化students屬性*/ public MapTest(){ this.students=new HashMap<String,Student>(); } /** * 測試添加:輸入學生ID,判斷是否被佔用 * 若薇被佔用,則輸入姓名,建立新學生對象,並且 * 添加到student中 */ 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; } } } /** * @param args測試Map的keySet方法 */ public void testKeySet(){ //keySet方法,返回Map中的所有“鍵”的Set集合 Set<String> keySet=students.keySet(); //取得student的容量 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); } } } /** * 測試刪除Map中的映射 */ public void testRemove(){ //擷取從鍵盤輸入待刪除學生ID字串 Scanner console=new Scanner(System.in); while (true) { //提示輸入待刪除的學生ID System.out.println("請輸入要刪除的學生ID"); String ID=console.next(); //判斷該ID是否有對應的學生對象 Student st=students.get(ID); if (st==null) { //提示輸入的ID並不存在 System.out.println("該ID不存在"); continue; } students.remove(ID); System.out.println("成功刪除學生"+st.name); break; } } /** * 通過entrySet方法來遍曆Map */ public void testEntrySet(){ //通過entrySet方法,返回Map中的所有索引值對 Set<Entry<String,Student>> entrySet=students.entrySet(); for (Entry<String, Student> entry : entrySet) { System.out.println("取得鍵"+entry.getKey()); System.out.println("對應的職為:"+entry.getValue().name); } } /** * 利用put方法修改Map中的已有映射 */ public void testModify(){ //提示輸入要修改的學生ID System.out.println("請輸入要修改的學生ID:"); //建立Scanner對象,去擷取從鍵盤上輸入的學生ID字串 Scanner console=new Scanner(System.in); while(true){ //取得從鍵盤上輸入的學生ID String stuID=console.next(); //從students中尋找該學生ID對應的學生對象 Student student=students.get(stuID); if (student==null) { System.out.println("該ID不存在,重新輸入!"); continue; } //提示當前對應的學生對象的姓名 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; } } /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub MapTest mt=new MapTest(); mt.testPut(); mt.testKeySet(); /*mt.testRemove(); mt.testEntrySet();*/ mt.testModify(); mt.testEntrySet(); }}
Java中的集合架構(中)