Map的两种遍历方法
生活随笔
收集整理的這篇文章主要介紹了
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的两种遍历方法的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Arvixe vs.SiteGround
- 下一篇: 2018蓝桥杯省赛---java---B