日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Map的两种遍历方法

發布時間:2023/12/3 编程问答 17 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Map的两种遍历方法 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
COPY/*** HashMap的使用* 存儲結構:哈希表(數組+鏈表+紅黑樹)*/public class Demo2 {public static void main(String[] args) {HashMap<Student, String> hashMap=new HashMap<Student, String>();Student s1=new Student("tang", 36);Student s2=new Student("yu", 101);Student s3=new Student("he", 10);//1.添加元素hashMap.put(s1, "成都");hashMap.put(s2, "杭州");hashMap.put(s3, "鄭州");//添加失敗,但會更新值hashMap.put(s3,"上海");//添加成功,不過兩個屬性一模一樣;//注:假如相同屬性便認為是同一個對象,怎么修改?hashMap.put(new Student("he", 10),"上海");System.out.println(hashMap.toString());//2.刪除元素hashMap.remove(s3);System.out.println(hashMap.toString());//3.遍歷//3.1 使用keySet()遍歷for (Student key : hashMap.keySet()) {System.out.println(key+" "+hashMap.get(key));}//3.2 使用entrySet()遍歷for (Entry<Student, String> entry : hashMap.entrySet()) {System.out.println(entry.getKey()+" "+entry.getValue());}//4.判斷//注:同上System.out.println(hashMap.containsKey(new Student("he", 10)));System.out.println(hashMap.containsValue("成都"));}}

總結

以上是生活随笔為你收集整理的Map的两种遍历方法的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。