集合(21):Map接口的子类----HashMap类
HashMap类案例
(1)HashMap<String,String>
(2)HashMap<Integer,String>
(3)HashMap<String,Student>
(4)HashMap<Student,String>
(1)HashMap<String,String>
import java.util.HashMap;
import java.util.Set;
/*
遍历:
步骤1:获取Map集合中所有映射的键的Set集合
步骤2:遍历键的集合,根据键找对应的值
步骤3:输出
*/
public class MapDemo3 {
public static void main(String[] args) {
HashMap<String, String> ssh = new HashMap<>();
//添加元素
ssh.put("洛", "霞");
ssh.put("卢锡安", "塞纳");
ssh.put("蛮王", "艾希");
ssh.put("盖伦", "卡特");
ssh.put("卢锡安", "塞纳");
//获取Map集合中所有的key组成的集合
Set<String> keySet = ssh.keySet();
for (String key : keySet) {
String value = ssh.get(key);
System.out.println(key + "---" + value);
}
}
}
执行结果如下:
卢锡安---塞纳
洛---霞
蛮王---艾希
盖伦---卡特
Process finished with exit code 0
(2)HashMap<Integer,String>
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
/*
HashMap<Integer,String>
*/
public class MapDemo5 {
public static void main(String[] args) {
//创建Map集合对象
HashMap<Integer, String> map = new HashMap<>();
map.put(1001, "朱佳乐");
map.put(1002, "刘生发");
map.put(1003, "张咪");
map.put(1004, "吕常福");
//遍历集合
System.out.println("====方式1:根据键找值====");
//获取Map集合中所有的key组成的集合
Set<Integer> keys = map.keySet();
for (Integer key : keys) {
String value = map.get(key);
System.out.println(key + "---" + value);
}
System.out.println("====方式2:根据键值对找键和值====");
Set<Map.Entry<Integer, String>> keyValue = map.entrySet();
for (Map.Entry<Integer, String> kv : keyValue) {
Integer key = kv.getKey();
String value = kv.getValue();
System.out.println(key + "---" + value);
}
}
}
执行结果如下:
====方式1:根据键找值====
1001---朱佳乐
1002---刘生发
1003---张咪
1004---吕常福
====方式2:根据键值对找键和值====
1001---朱佳乐
1002---刘生发
1003---张咪
1004---吕常福
Process finished with exit code 0
(3)HashMap<String,Student>
//创建一个学生类
import java.util.Objects;
public class Student4 {
private String name;
private int age;
public Student4() {
}
public Student4(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 String toString() {
return "Student{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Student4 student4 = (Student4) o;
return age == student4.age &&
Objects.equals(name, student4.name);
}
@Override
public int hashCode() {
return Objects.hash(name, age);
}
}
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
/*
HashMap<String,Student>
*/
public class MapDemo6 {
public static void main(String[] args) {
//创建map集合对象
HashMap<String, Student4> map = new HashMap<>();
//创建4个学生对象
Student4 s1 = new Student4("朱佳乐", 18);
Student4 s2 = new Student4("刘生发", 17);
Student4 s3 = new Student4("张咪", 19);
Student4 s4 = new Student4("张梦云", 19);
//向map集合中添加键值对
map.put("帅气男孩", s1);
map.put("亿气风发", s2);
map.put("犹抱琵琶半遮面", s3);
map.put("仙女下凡", s4);
//获取键值对
Set<Map.Entry<String, Student4>> entries = map.entrySet();
//遍历
for (Map.Entry<String, Student4> keyValue : entries) {
String key = keyValue.getKey();//获取Map集合中所有的key组成的集合
Student4 value = keyValue.getValue();//获取key组成的集合中的学生信息
System.out.println(key + "---" + value);
}
}
}
执行结果如下:
帅气男孩---Student{name='朱佳乐', age=18}
仙女下凡---Student{name='张梦云', age=19}
亿气风发---Student{name='刘生发', age=17}
犹抱琵琶半遮面---Student{name='张咪', age=19}
Process finished with exit code 0
(4)HashMap<Student,String>
//创建一个学生类
import java.util.Objects;
public class Student4 {
private String name;
private int age;
public Student4() {
}
public Student4(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 String toString() {
return "Student{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Student4 student4 = (Student4) o;
return age == student4.age &&
Objects.equals(name, student4.name);
}
@Override
public int hashCode() {
return Objects.hash(name, age);
}
}
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
/*
HashMap<Student,String>
*/
public class MapDemo7 {
public static void main(String[] args) {
HashMap<Student4, String> map = new HashMap<>();
//创建学生对象
Student4 s1 = new Student4("貂蝉", 17);
Student4 s2 = new Student4("西施", 18);
Student4 s3 = new Student4("杨玉环", 16);
Student4 s4 = new Student4("王昭君", 19);
Student4 s5 = new Student4("貂蝉", 17);
//将元素添加到集合中
map.put(s1, "1111");
map.put(s2, "2222");
map.put(s3, "3333");
map.put(s4, "4444");
map.put(s5, "5555");
//遍历
Set<Map.Entry<Student4, String>> entries = map.entrySet();
for (Map.Entry<Student4, String> kv : entries) {
Student4 key = kv.getKey();
String value = kv.getValue();
System.out.println(key + "---" + value);
}
}
}
执行结果如下:
Student{name='杨玉环', age=16}---3333
Student{name='貂蝉', age=17}---5555
Student{name='王昭君', age=19}---4444
Student{name='西施', age=18}---2222
Process finished with exit code 0