生活随笔
收集整理的這篇文章主要介紹了
mooc_java 集合框架中 学生所选课程2MapHashMap
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
Map&HashMap
Map提供映射關(guān)系,元素以鍵值對(duì)形式存儲(chǔ),Map的鍵值對(duì)一Entry類型的對(duì)象實(shí)例形式存在,key值不能重復(fù),value可以
鍵最多能映射到一個(gè)值,支持泛型 Map<K,V>
HashMap是Map的一個(gè)重要實(shí)現(xiàn)類,基于哈希表實(shí)現(xiàn),其中的Entry對(duì)象是無序排列的,key和value值都可以為null,但只能有一個(gè)key值為null的映射(key值不可以重復(fù))
public class MapTest {/*** 承裝學(xué)生類型對(duì)象*/public Map<String, Student>
students;public MapTest(){this.students=
new HashMap<String,Student>
();}/*** 測(cè)試添加:輸入學(xué)生Id,判斷是否被占用* 若為被占用,則輸入姓名,創(chuàng)建學(xué)生對(duì)象,并且添加到students中* @param args*/public void testPut(){//創(chuàng)建Scanner對(duì)象,用來獲取輸入的學(xué)生ID和姓名Scanner console=
new Scanner(System.in);int i=0
;while(i<3
){System.out.println("請(qǐng)輸入學(xué)生ID"
);String ID=
console.next();//判斷ID是否被占用Student st=
students.get(ID);if(st==
null){System.out.println("請(qǐng)輸入學(xué)生姓名:"
);String name=
console.next();Student news=
new Student(ID, name);//調(diào)用students的put方法,添加ID-學(xué)生映射
students.put(ID,news);System.out.println("成功添加學(xué)生:"+
students.get(ID).name); i++
;}else{System.out.println("該學(xué)生ID已被占用"
);continue;}}}/*** 測(cè)試Map的keySet方法* @param args*/public void testKeySet(){//已經(jīng)規(guī)定泛型為String類型Set<String> ks=
students.keySet();//取得students的容量System.out.println("總共有:"+students.size()+"個(gè)學(xué)生"
);//遍歷KeySet,取得每一個(gè)鍵,再調(diào)用個(gè)體方法取得每個(gè)鍵對(duì)應(yīng)的valuefor(String stuId:ks){Student st=
students.get(stuId);if(st!=
null)System.out.println("學(xué)生:"+
st.name);}}/*** 測(cè)試刪除Map中的映射* @param args*/public void testRemove(){//獲取從鍵盤輸入的待刪除學(xué)生ID字符串Scanner console=
new Scanner(System.in);while(
true){System.out.println("輸入要?jiǎng)h除的學(xué)生ID"
);String stuID=
console.next();//判斷是否有對(duì)應(yīng)的學(xué)生對(duì)象Student st=
students.get(stuID);if(st==
null){//提示輸入的ID并不存在System.out.println("該ID不存在"
);continue;}students.remove(stuID);System.out.println("成功刪除學(xué)生"+
st.name);break;}}/*** 通過entrySet方法來遍歷Map*/public void testEntrySet(){//entrySet方法返回Map的所有鍵值對(duì)//Set<Entry> es=students.entrySet();Set<Entry<String,Student>> es=
students.entrySet();for(Entry<String,Student>
entry:es){System.out.println("取得鍵為:"+
entry.getKey());System.out.println("值為為:"+
entry.getValue().name); }}/*** put方法修改Map中 已有的映射* @param args*/public void testModify(){System.out.println("請(qǐng)輸入要修改的學(xué)生的ID:"
);Scanner console=
new Scanner(System.in);while(
true){String stuID=
console.next();//判斷是否有對(duì)應(yīng)的學(xué)生對(duì)象Student st=
students.get(stuID);if(st==
null){//提示輸入的ID并不存在System.out.println("該ID不存在"
);continue;}else{System.out.println("當(dāng)前學(xué)生ID對(duì)應(yīng):"+
st.name);System.out.println("請(qǐng)輸入新的學(xué)生姓名 :"
);String name=
console.next();Student ns=
new Student(stuID, name);students.put(stuID, st);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();}} ?
轉(zhuǎn)載于:https://www.cnblogs.com/mao-19/p/4961684.html
總結(jié)
以上是生活随笔為你收集整理的mooc_java 集合框架中 学生所选课程2MapHashMap的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。