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());
}
}
}
}