淺入深出之Java集合架構(中),java集合架構
Java中的集合架構(中)
由於Java中的集合架構的內容比較多,在這裡分為三個部分介紹Java的集合架構,內容是從淺到深,如果已經有java基礎的小夥伴可以直接跳到<淺入深出之Java集合架構(下)>。
目 錄
淺入深出之Java集合架構(上)
淺入深出之Java集合架構(中)
淺入深出之Java集合架構(下) 努力趕製中。。關注後更新會提醒哦!
前 言
在<淺入深出之Java集合架構(上)>中介紹了List介面和Set介面的基本操作,在這篇文章中,我將介紹關於Map介面的基本操作。使用的樣本是在<淺入深出之Java集合架構(上)>中的類比學生選課的小程式,不清楚的朋友可以先去閱讀<淺入深出之Java集合架構(上)>。
一、Map&HashMap簡介1)Map介面
1. Map介面提供了一中映射關係,其中的元素是索引值對(key-value)的形式儲存的,能夠實現根據Key快速尋找value。Key-value可以是任何對象,是以Entry類型的對象執行個體存在的。
2.Key是不可以重複的,Value是可以重複的。Key-value都可以為null,不過只能有一個key是null。
3.Map支援泛型,Map<K,V>
4.每個鍵最多隻能映射到一個值
5.Map介面提供了分別返回key值集合、value值集合以及Entry(索引值對)集合的方法
6.通過put<K key,V value>,remove<Object key>運算元
2)HashMap實作類別
1. HashMap中的Entry對象是無序排列的,HashMap是Map的一個重要實作類別,也是最常用的,基於雜湊表是實現
2. Key值和value值都可以為null,但是一個HashMap只能有一個key值為null的映射(key不可重複)
二、學生選課——使用Map新增學生案例功能說明
1.通過Map<String,Student>進行學生資訊管理,其中key為學生ID,value為學生對象。
2.通過鍵盤輸入學生資訊
3.對集合中的學生資訊進行增刪該查操作
首先建立一個StuMap類來測試Map的使用方法。如下:
1 /** 2 * 學生類的Map集合類 3 * 4 * @author acer 5 * 6 */ 7 public class StuMap { 8 // 用來承裝學生類型對象 9 private Map<String, Student> students;10 private static Scanner in;11 {12 in = new Scanner(System.in);13 }14 15 public StuMap() {16 students = new HashMap<String, Student>();17 18 }19 //省略方法,下面的方法會逐個列出20 }
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
和List介面不同,向Map添加對象使用的是put(key,value)方法。以下是使用樣本:
1 /* 2 * 新增學生類 輸入學生id, 3 * 判斷是否被佔用 若未被佔用,則輸入姓名,建立新的學生對象,並且把該對象添加到Map中 4 * 否則,則提示已有該id 5 */ 6 public void AddStu() { 7 System.out.println("請輸入要添加的學生id:"); 8 String Id = in.next();// 接受輸入的id 9 Student st=students.get(Id);10 if(st==null){11 12 System.out.println("請輸入要添加的學生姓名:");13 String name = in.next();// 接受輸入的name14 this.students.put(Id, new Student(Id, name));15 }else{16 System.out.println("此Id已被佔用!");17 }18 19 }
再寫一個列印輸出的測試函數,如:
1 /* 2 * 列印學生類 3 * 4 */ 5 public void PrintStu() { 6 System.out.println("總共有"+this.students.size()+"名學生:"); 7 //遍曆keySet 8 for (String s : this.students.keySet()) { 9 Student st=students.get(s);10 if(st!=null){11 System.out.println("學生:" + students.get(s).getId() + "," + students.get(s).getName());12 }13 }14 }
上面的例子是使用Map的keySet()返回Map中的key的Set集合再用if進行判斷輸出,在Map還可以用entrySet()的方法返回Map中的索引值對Entry,如:
1 /* 2 * 通過entrySet方法遍曆Map 3 */ 4 public void EntrySet(){ 5 Set<Entry<String, Student>> entrySet =students.entrySet(); 6 for(Entry<String, Student> entry:entrySet){ 7 System.out.println("取得建:"+entry.getKey()); 8 System.out.println("對應的值:"+entry.getValue().getName()); 9 10 }11 }
最後我們用主函數調用一下這些函數來看看效果如何
1 public static void main(String[] args) {2 StuMap stu = new StuMap();3 for (int i = 0; i < 3; i++) {4 stu.AddStu();5 }6 stu.PrintStu();7 stu.EntrySet();8 }
程式碼分析:
1.student.get(ID)是採用Map的get()方法,檢測是否存在值為ID的學生,如果沒有,則返回null。這裡的案例是把Map中的key值設為學生的ID值,所以可以用這樣的方式來檢測,如果key值是學生其他屬則性另當別論!!
2.keySet()方法,返回所有鍵的Set集合。
3.keyset()返回Map中所有的key以集合的形式可用Set集合接收,HashMap當中的映射是無序的。
3.Map還可以用entrySet()的方法返回Map中的索引值對Entry,Entry也是Set集合,它可以調用getKey()和getValue()方法來分別得到索引值對的“鍵”和“值”。
運行結果:
三、學生選課——刪除Map中的學生
刪除Map中的索引值對是調用remove(object key)方法,下面是它的使用樣本:
1 /* 2 * 刪除map中映射 3 */ 4 public void RemoveStu(){ 5 do{ 6 System.out.println("請輸入要刪除的學生id:"); 7 String Id = in.next();// 接受輸入的id 8 Student st=students.get(Id); 9 if(st==null){10 System.out.println("此id不存在!");11 12 }else{13 this.students.remove(Id);14 System.out.println("成功刪除"+st.getId()+","+st.getName()+"同學");15 break;16 }17 }while(true);18 }
運行結果:
四、學生選課——修改Map中的學生
修改Map中的索引值對有兩種方法,第一種就是用put方法。其實就是添加方法中的put,使用方法跟添加無異,這裡的本質就是利用put把原來的資料給覆蓋掉,即修改。
1 /* 2 * 利用put方法修改Map中的value值 3 */ 4 public void ModifyStu(){ 5 do{ 6 System.out.println("請輸入要修改的學生id:"); 7 String Id = in.next();// 接受輸入的id 8 Student st=students.get(Id); 9 if(st==null){10 System.out.println("此id不存在!");11 12 }else{13 System.out.println("學生原來的姓名:"+st.getName()+",請輸入修改後的姓名:");14 String name = in.next();// 接受輸入的name15 st=new Student(Id,name);16 this.students.put(Id,st);17 System.out.println("成功修改!修改後的學生為:"+st.getId()+","+st.getName()+"同學");18 break;19 }20 }while(true);21 22 }
除了用put方法外,Map中提供一個叫做replace的方法,知名知其意,就是“替換”的意思。replace的方法使用和put方法一樣,這是因為它的內部源碼如下:
1 if (map.containsKey(key)) {2 return map.put(key, value);3 } else4 return null;5
可以看出replace方法就是調用put方法來完成修改操作的,但是我們為了和添加put進行區分,最好在使用修改的時候用replace方法進行修改。這樣的代碼可讀性和維護性就增強了。
那麼使用replace修改Map中的value值如下:(推薦使用replace方法)
1 /* 2 * 利用replace方法修改Map中的value值 3 */ 4 public void Modify(){ 5 do{ 6 System.out.println("請輸入要修改的學生id:"); 7 String Id = in.next();// 接受輸入的id 8 Student st=students.get(Id); 9 if(st==null){10 System.out.println("此id不存在!");11 12 }else{13 System.out.println("學生原來的姓名:"+st.getName()+",請輸入修改後的姓名:");14 String name = in.next();// 接受輸入的name15 st=new Student(Id,name);16 this.students.replace(Id, st);17 System.out.println("成功修改!修改後的學生為:"+st.getId()+","+st.getName()+"同學");18 break;19 }20 }while(true);21 }
運行結果:
五、總結
Map -特點:元素成對出現,key-value,是映射關係,key不能重複,但value可以重複,也就是說,可以多key對一個value。支援泛型如Map<yy1, yy2>。
-實作類別:HashMap是最常用的,HashMap中是無序排列,其元素中key或value可為null(但只能有一個為null)。
-聲明(泛型)舉例: 在類中聲明 public Map<類型1, 類型2> xxx; 然後再構造方法中this.xxx = new HashMap<類型1, 類型2();
-擷取:yy temp = xxx.get(key)
-添加:xxx.put( key(xx型), zz(yy型) );
-返回map中所有key(返回是set型集合形式) set xxxxx = xxx.keyset(); 用於遍曆。
-返回map中所有entry對(key-value對)(返回是set型集合形式) set<Entry<xx, yy>> xxxxx = xxx.entrySet(); 同樣用於遍曆。 遍曆時:for(Entry<xx,yy> 元素: xxxxx)
-刪除:xxx.remove(key);
-修改:可以用put,當put方法傳入的key存在就相當於是修改(覆蓋);但是推薦使用replace方法!
本章主要介紹了Map介面的基本操作,發出下一篇預告:集合的基本操作不夠用怎麼辦?如何判斷集合中的某個元素對象是否存在?如何對List集合進行排序呢?這些問題是否困擾著你呢,在下一篇中博主將會針對這些問題來給大家介紹java集合架構的其他成員,後續如何請關注~~