集合之Map【HashMap】
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 | 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()); } } } |
材料:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 | 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; } } |
综合小练习:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 | 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()); } } } } |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)
· winform 绘制太阳,地球,月球 运作规律