版权声明:转载请注明原始链接 https://blog.csdn.net/sswqzx/article/details/82875701
1、概述
Map集合:由一系列键值对映射组成的集合、就像php中关系数组。
Map是一个接口、它的实现类有HashMap、LinkedHashMap
Map常用方法:
2、常用方法
package com.blog.test;
import java.util.HashMap;
public class Test {
public static void main(String[] args) {
// 1. 创建一个映射表集合对象
// 哈希表是需要根据 `键值对对象的key` 来计算存储下标的. 与值没有关系.
HashMap<String, Integer> map = new HashMap<>();
// 2. 存储映射表对象
map.put("大罗", 18);
map.put("小罗", 22);
map.put("卡卡", 17);
map.put("小小罗", 20);
map.put("梅西", 28);
// 修改 :
map.put("大罗", 26); // 如果键相同, 新值就会覆盖旧值.
System.out.println("map = " + map);
// 删除 : 键值对对象.
Integer value = map.remove("梅西"); // 如果 key 不存在, 返回 null
System.out.println("value = " + value);
value = map.remove("梅西"); // 如果 key 存在, 则返回该 key 对象的 value
System.out.println("value = " + value);
System.out.println("map = " + map);
// 查询 : (遍历)
Integer value2 = map.get("小罗");
System.out.println("小罗 = " + value2);
}
}
3、键找值遍历
键找值方式:即通过元素中的键,获取键所对应的值。方法: keyset()
package com.blog.test;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Set;
public class Test {
public static void main(String[] args) {
// 1. 创建一个映射表集合对象
// 哈希表是需要根据 `键值对对象的key` 来计算存储下标的. 与值没有关系.
HashMap<String, Integer> map = new HashMap<>();
// 2. 存储映射表对象
map.put("大罗", 18);
map.put("小罗", 22);
map.put("卡卡", 17);
map.put("小小罗", 20);
map.put("梅西", 28);
System.out.println("map = " + map);
System.out.println("-----------");
// 3. 遍历映射表集合 (方式一 : 通过键找值)
// 3.1 获取映射表中所有键的集合
Set<String> keySet = map.keySet();
// 3.2 使用迭代器
Iterator<String> it = keySet.iterator();
while (it.hasNext()) {
String key = it.next();
// 3.3 使用映射表调用 get 方法, 传入 key, 获取 value
Integer value = map.get(key);
System.out.println(key + " = " + value);
}
}
}
4、键值对遍历
public Set<Map.Entry<K,V>> entrySet() : 获取到Map集合中所有的键值对对象的集合(Set集合)
public K getKey() :获取Entry对象中的键。
public V getValue() :获取Entry对象中的值。
package com.blog.test;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
public class Test {
public static void main(String[] args) {
// 1. 创建一个映射表集合对象
// 哈希表是需要根据 `键值对对象的key` 来计算存储下标的. 与值没有关系.
HashMap<String, Integer> map = new HashMap<>();
// 2. 存储映射表对象
map.put("大罗", 18);
map.put("小罗", 22);
map.put("卡卡", 17);
map.put("小小罗", 20);
map.put("梅西", 28);
System.out.println("map = " + map);
System.out.println("-----------");
// 3. 遍历映射表集合 (方式二 : 根据键值对对象查找对应的 key 和 value)
// 3.1 获取映射表的键值对集合对象
Set<Map.Entry<String, Integer>> entrySet = map.entrySet();
// 3.2 遍历 entrySet 集合
for (Map.Entry<String, Integer> entry : entrySet) {
// 3.3 使用 entry 对象调用 getKey() 和 getValue() 获取键和值
String key = entry.getKey();
Integer value = entry.getValue();
System.out.println(key + " = " + value);
}
}
}
5、HashMap
HashMap:存储数据采用的哈希表结构,元素的存取顺序不能保证一致。由于要保证键的唯一、不重复,需
要重写键的hashCode()方法、equals()方法。
学生姓名相同并且年龄相同视为同一名学生。
学生类:
public class 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);
}
}
测试类 :
public class MapKeyTest4 {
public static void main(String[] args) {
// 需求 : 自定义对象作为 HashMap 的 key 使用.
// Map 的特点 : key 不能重复, 如果 key 相同, 新值就会覆盖旧值.
// 如果自定义对象作为 HashMap 的 key, 那么该自定义对象所属的类就必须重写 `hashCode + equals` 方法. 从而来保证 key 的唯一性.
HashMap<Student, String> map = new HashMap<>();
// 存储映射对象
map.put(new Student("王昭君", 16), "东京");
map.put(new Student("西施", 17), "南京");
map.put(new Student("杨玉环", 19), "北京");
map.put(new Student("貂蝉", 18), "苏州");
map.put(new Student("王昭君", 16), "西京");
// 遍历一 (keySet + iterator)
Set<Student> keySet = map.keySet();
Iterator<Student> it = keySet.iterator();
while (it.hasNext()) {
Student stu = it.next();
String address = map.get(stu);
System.out.println(stu + " -> " + address);
}
System.out.println("----------------");
// 遍历二 (EntrySet + Foreach)
// Set<Map.Entry<Student, String>> entrySet = map.entrySet();
for (Map.Entry<Student, String> entry : map.entrySet()) {
// Student key = entry.getKey();
// String value = entry.getValue();
System.out.println(entry.getKey() + " = " + entry.getValue());
}
}
}
6、LinkedHashMap
LinkedHashMap:HashMap下有个子类LinkedHashMap,存储数据采用的哈希表结构+链表结构。通过链
表结构可以保证元素的存取顺序一致;通过哈希表结构可以保证的键的唯一、不重复,需要重写键的
hashCode()方法、equals()方法。
public class MapKeyTest4 {
public static void main(String[] args) {
// 需求 : 自定义对象作为 HashMap 的 key 使用.
// Map 的特点 : key 不能重复, 如果 key 相同, 新值就会覆盖旧值.
// 如果自定义对象作为 HashMap 的 key, 那么该自定义对象所属的类就必须重写 `hashCode + equals` 方法. 从而来保证 key 的唯一性.
HashMap<Student, String> map = new LinkedHashMap<>();
// 存储映射对象
map.put(new Student("王昭君", 16), "东京");
map.put(new Student("西施", 17), "南京");
map.put(new Student("杨玉环", 19), "北京");
map.put(new Student("貂蝉", 18), "苏州");
map.put(new Student("王昭君", 16), "西京");
// 遍历一 (keySet + iterator)
Set<Student> keySet = map.keySet();
Iterator<Student> it = keySet.iterator();
while (it.hasNext()) {
Student stu = it.next();
String address = map.get(stu);
System.out.println(stu + " -> " + address);
}
System.out.println("----------------");
// 遍历二 (EntrySet + Foreach)
// Set<Map.Entry<Student, String>> entrySet = map.entrySet();
for (Map.Entry<Student, String> entry : map.entrySet()) {
// Student key = entry.getKey();
// String value = entry.getValue();
System.out.println(entry.getKey() + " = " + entry.getValue());
}
}
}
练习
package com.blog.test;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
import java.util.Set;
public class Test {
public static void main(String[] args) {
System.out.println("请录入一个字符串:");
String line = new Scanner(System.in).nextLine();
// 定义 每个字符出现次数的方法
findChar(line);
}
private static void findChar(String line) {
//1:创建一个集合 存储 字符 以及其出现的次数
HashMap<Character, Integer> map = new HashMap<Character, Integer>();
//2:遍历字符串
for (int i = 0; i < line.length(); i++) {
char c = line.charAt(i);
//判断 该字符 是否在键集中
if (!map.containsKey(c)) {//说明这个字符没有出现过
//那就是第一次
map.put(c, 1);
} else {
//先获取之前的次数
Integer count = map.get(c);
//count++;
//再次存入 更新
map.put(c, ++count);
}
}
System.out.println(map);
}
}
7、HashSet与HashMap关系
8、JDK9对集合添加元素优化
List, Set, Map 接口的特有静态方法 : of(E …);
public class OfTest1 {
public static void main(String[] args) {
// 方式一 : Collection 集合定义的 add 方法.
ArrayList<Integer> list1 = new ArrayList<>();
list1.add(10);
list1.add(20);
list1.add(30);
System.out.println("list1 = " + list1);
// 方式二 : Collections 集合工具类定义的 addAll 方法.
ArrayList<Integer> list2 = new ArrayList<>();
Collections.addAll(list2, 10, 20, 30);
list2.add(40);
System.out.println("list2 = " + list2);
// 方式三 : List 接口定义的 of 静态方法
// 局限性 : 元素不可以进行 `增删改` 的操作
List<Integer> list3 = List.of(10, 20, 30);
// list3.add(40); UnsupportedOperationException 不支持的操作异常.
System.out.println("list3 = " + list3);
// 除了 List, Set 和 Map 接口也拥有 of 静态方法.
Set<Integer> set = Set.of(10, 20, 30, 40, 50);
System.out.println("set = " + set);
Map<String, Integer> map = Map.of("Jack", 18, "Rose", 16, "Peter", 20, "Lucy", 17);
System.out.println("map = " + map);
}
}