集合之Map【HashMap】
阿新 • • 發佈:2022-12-12
package com.Lucky.Map; import java.util.Iterator; import java.util.Map; import java.util.Set; import java.util.function.BiConsumer; /* HashMap: 1.底層結構是雜湊表 2.依賴hashCode以及equals方法保證鍵的唯一 3.如果鍵儲存的是“自定義物件”,就要重寫hashCode方法以及equals方法 4.如果值儲存的是:自定義物件,就不用重寫上面兩個方法 */ public class HashMap { public static void main(String[] args) { Map<Student,String> map=new java.util.HashMap<>(); Student str1=new Student("唯一",22); Student str2=new Student("唯二",20); Student str3=new Student("唯三",22); Student str4=new Student("唯三",22); map.put(str1,"漢族"); map.put(str2,"壯族"); map.put(str3,"黎族"); map.put(str4,"黎族"); //新增失敗 // System.out.println(map); // // map.forEach(new BiConsumer<Student, String>() { // @Override // public void accept(Student student, String s) { // System.out.println(student); // System.out.println(s); // } // }); map.forEach((stu,res)-> System.out.println(stu+":"+res)); System.out.println("-----------------增強for迴圈-------------------"); Set<Map.Entry<Student, String>> entries = map.entrySet(); for (Map.Entry<Student, String> entry : entries) { System.out.println(entry.getKey()+":"+entry.getValue()); } System.out.println("-----------------迭代器-------------------"); Iterator<Map.Entry<Student, String>> iterator = entries.iterator(); while (iterator.hasNext()){ System.out.println(iterator.next()); } } }
材料:
package com.Lucky.Map; import java.util.Objects; public class Student implements Comparable<Student>{ private String name; private int age; public Student() { } public Student(String name, int age) { this.name = name; this.age = age; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } @Override public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; Student student = (Student) o; return age == student.age && Objects.equals(name, student.name); } @Override public int hashCode() { return Objects.hash(name, age); } @Override public String toString() { return "Student{" + "name='" + name + '\'' + ", age=" + age + '}'; } @Override public int compareTo(Student o) { //比較年齡 int res=this.getAge()-o.getAge(); if(res==0){ //年齡相等,就比較姓名 res = this.getName().compareTo(o.getName()); } return res; } }
綜合小練習:
package com.Lucky.Map; import java.util.*; import java.util.HashMap; /** * 案例: 組織80人去春遊,有ABCD四個景點可以投票,統計出最多人想去的地方 */ public class HashMapDemo1 { public static void main(String[] args) { //建立景點 String[] arr={"A","B","C","D"}; //建立ArrayList集合統計統計結果 ArrayList<String> list=new ArrayList<>(); //建立隨機選擇的地方 Random random=new Random(); for (int i = 0; i < 80; i++) { int index = random.nextInt(arr.length); //獲取隨機索引 list.add(arr[index]); //將隨機索引的值新增到list集合中 } //定義一個HashMap集合 Map<String,Integer> map=new HashMap<>(); //遍歷list集合 for (String jd : list) { //判斷Map集合中是否存在該景點 if(map.containsKey(jd)){ //存在 //獲取該景點的選擇次數 int re=map.get(jd); //景點選擇數量+1 re++; //新增到集合中 map.put(jd,re); }else { //不存在 map.put(jd,1); } } System.out.println(map); //求最多人選擇的景點 int MAX=0; Set<Map.Entry<String, Integer>> entries = map.entrySet(); for (Map.Entry<String, Integer> entry : entries) { int count=entry.getValue(); if(count>MAX){ MAX=count; } } for (Map.Entry<String, Integer> entry : entries) { int count=entry.getValue(); if(count==MAX){ System.out.println("投票人數最多景點:"+entry.getKey()); } } } }