Map集合
-
Map集合概述
interface Map<K,V> K:键的类型;V:值的类型
-
Map集合的特点
-
双列集合,一个键对应一个值
-
键不可以重复,值可以重复
-
Map集合的基本使用
public class MapDemo01 {
public static void main(String[] args) {
//创建集合对象
Map<String,String> map = new HashMap<String,String>();
//V put(K key, V value) 将指定的值与该映射中的指定键相关联
map.put("001","林青霞");
map.put("002","张曼玉");
map.put("003","王祖贤");
map.put("003","柳岩");
//输出集合对象
System.out.println(map);
}
}
Map集合的基本功能【应用】
方法介绍:
方法名 | 说明 |
---|---|
V put(K key,V value) | 添加元素 |
V remove(Object key) | 根据键删除键值对元素 |
void clear() | 移除所有的键值对元素 |
boolean containsKey(Object key) | 判断集合是否包含指定的键 |
boolean containsValue(Object value) | 判断集合是否包含指定的值 |
boolean isEmpty() | 判断集合是否为空 |
int size() | 集合的长度,也就是集合中键值对的个数 |
public class Map集合使用 {
public static void main(String[] args) {
Map<String,Integer> m = new HashMap<>();
//put细节:put是有返回值的
//如果键一样的时候,会覆盖掉值,并返回,如果不一样则返回Null;
m.put("老六",18);
m.put("蔡徐坤",19);
m.put("肖战",18);
m.put("王一博",18);
//打印
System.out.println(m);//{蔡徐坤=19, 肖战=18, 王一博=18}
//删除,只用到键
m.remove("老六");
//判断是否包含键或者值的两个方法;
Boolean r1 = m.containsKey("蔡徐坤");
Boolean r2 = m.containsValue(18);
System.out.println(r1);//true
System.out.println(r2);//true
//清除集合;
m.clear();
//判断集合是否为空
Boolean r3 = m.isEmpty();
System.out.println(r3);//true
}
}
Map集合三种遍历方式
第一种:
- - 遍历思路
- - 我们刚才存储的元素都是成对出现的,所以我们把Map看成是一个夫妻对的集合
- - 把所有的丈夫给集中起来
- - 遍历丈夫的集合,获取到每一个丈夫
- - 根据丈夫去找对应的妻子
- - 步骤分析
- - 获取所有键的集合。用keySet()方法实现
- - 遍历键的集合,获取到每一个键。用增强for实现
- - 根据键去找值。用get(Object key)方法实现
- - 代码实现
package Map集合;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
public class Map集合遍历 {
public static void main(String[] args) {
Map<String,Integer> m = new HashMap<>();
m.put("肖战",18);
m.put("王一博",19);
m.put("蔡徐坤",12);
m.put("范丞丞",13);
//获取所有键集合。利用keySet方法实现
Set<String> keys= m.keySet();
//用增强for遍历键的集合,获取每一个键
for (String key: keys) {
//利用键获取值.get(Object key)方法实现
Integer value = m.get(key);
System.out.println(key+"="+value);
}
}
}
第二种
- - 遍历思路
- - 我们刚才存储的元素都是成对出现的,所以我们把Map看成是一个夫妻对的集合
- - 获取所有结婚证的集合
- - 遍历结婚证的集合,得到每一个结婚证
- - 根据结婚证获取丈夫和妻子
- - 我们刚才存储的元素都是成对出现的,所以我们把Map看成是一个夫妻对的集合
- - 步骤分析
- - 获取所有键值对对象的集合
- - Set<Map.Entry<K,V>> entrySet():获取所有键值对对象的集合
- - 遍历键值对对象的集合,得到每一个键值对对象
- - 用增强for实现,得到每一个Map.Entry
- - 根据键值对对象获取键和值
- - 用getKey()得到键
- - 用getValue()得到值
- - 代码实现
public class Map集合遍历 {
public static void main(String[] args) {
Map<String,Integer> m = new HashMap<>();
m.put("肖战",18);
m.put("王一博",19);
m.put("蔡徐坤",12);
m.put("范丞丞",13);
//获取所有键值对对象的集合
Set<Map.Entry<String,Integer>> entrySet = m.entrySet();
//遍历键值对对象的集合,得到每一个键值对对象
for (Map.Entry<String,Integer> me: entrySet) {
//根据键值对对象获取键和值
System.out.println(me.getKey()+"="+me.getValue());
}
}
}
第三种
Lambda表达式遍历,内部原理和第二种方式一样
public class Map集合遍历 {
public static void main(String[] args) {
Map<String,Integer> m = new HashMap<>();
m.put("肖战",18);
m.put("王一博",19);
m.put("蔡徐坤",12);
m.put("范丞丞",13);
m.forEach(new BiConsumer<String, Integer>() {
@Override
public void accept(String s, Integer integer) {
System.out.println(s + "=" + integer);
}
});
m.forEach((s,integer)-> System.out.println(s + "=" + integer));
}
}
代码原理实现:
default void forEach(BiConsumer<? super K, ? super V> action) {
Objects.requireNonNull(action);
for (Map.Entry<K, V> entry : entrySet()) {
K k;
V v;
try {
k = entry.getKey();
v = entry.getValue();
} catch (IllegalStateException ise) {
// this usually means the entry is no longer in the map.
throw new ConcurrentModificationException(ise);
}
action.accept(k, v);
}
}
HashMap集合
HashMap练习:
public class HashMap练习 {
public static void main(String[] args) {
HashMap<Student,String> hashMap = new HashMap<>();
hashMap.put(new Student("蔡徐坤",18),"广东");
hashMap.put(new Student("王一博",19),"江西");
hashMap.put(new Student("肖战",17),"北京");
hashMap.put(new Student("范丞丞",16),"浙江");
//重写了HashCode和equals就会去重
hashMap.put(new Student("范丞丞",16),"山东");
hashMap.forEach(new BiConsumer<Student, String>() {
@Override
public void accept(Student student, String s) {
System.out.println("学生名字:"+student.getName()+" 学生年龄:"+student.getAge()+" 学生籍贯:"+s);
}
});
}
}
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;
}
@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 void setAge(int age) {
this.age = age;
}
}
public class HashMap练习二 {
public static void main(String[] args) {
String[] arr = new String[]{"双月湾","白云山","欢乐谷","世界之窗"};
ArrayList<String> list = new ArrayList<>();
Random r = new Random();
for (int i = 0; i < 88; i++) {
int dex = r.nextInt(arr.length);
list.add(arr[dex]);
}
HashMap<String,Integer> hashMap = new HashMap<>();
for (String name : list) {
//hashMap.getOrDefault(name, 0) 的执行过程如下:
//如果 name 在 HashMap 中存在,则返回该键对应的值。
//如果 name 在 HashMap 中不存在,则返回默认值 0。
hashMap.put(name, hashMap.getOrDefault(name, 0) + 1);
}
System.out.println(hashMap);
int max = 0;
String maxkey = null;
Set<Map.Entry<String,Integer>> keys = hashMap.entrySet();
for (Map.Entry<String, Integer> key : keys) {
if (key.getValue()>max){
max = key.getValue();
maxkey = key.getKey();
}
}
System.out.println(max);
System.out.println(maxkey);
}
}
LinkedHashMap集合
public class LinkedHashMap集合 {
public static void main(String[] args) {
LinkedHashMap<String,Integer> l = new LinkedHashMap<>();
l.put("王一博",99);
l.put("蔡徐坤",15);
l.put("肖战",93);
l.put("范丞丞",18);
System.out.println(l);
}
}
TreeMap集合
- TreeMap集合概述和特点【理解】
- TreeMap底层是红黑树结构
- 依赖自然排序或者比较器排序,对键进行排序
- 如果键存储的是自定义对象,需要实现Comparable接口或者在创建TreeMap对象时候给出比较器排序规则
public class TreeMap集合 {
public static void main(String[] args) {
TreeMap<Integer,String> tm = new TreeMap<>(new Comparator<Integer>() {
@Override
public int compare(Integer o1, Integer o2) {
//o1是需要添加的数,o2已经在红黑树存在的数
return o2-o1;
}
});
tm.put(1,"a");
tm.put(2,"b");
tm.put(3,"c");
tm.put(4,"d");
tm.put(5,"e");
System.out.println(tm);
}
}
public class TreeMap集合练习 {
public static void main(String[] args) {
TreeMap<studen1,String> tm = new TreeMap<>();
tm.put(new studen1("abc",18),"江西");
tm.put(new studen1("cdas",23),"广东");
tm.put(new studen1("dsa",16),"浙江");
tm.put(new studen1("asad",79),"北京");
System.out.println(tm);
}
}
class studen1 implements Comparable<studen1>{
private String name;
private int age;
public studen1(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;
studen1 student1 = (studen1) o;
return age == student1.age && Objects.equals(name, student1.name);
}
@Override
public int hashCode() {
return Objects.hash(name, age);
}
@Override
public int compareTo(studen1 o) {
//返回值是负数则是小的,存左边,返回值是正数则是大的,存右边,返回值0不存
int i = this.getAge() - o.getAge();
if (this.getAge() == o.getAge()){
if (Objects.equals(this.getName(), o.getName())){
return 0;
}else
return this.getName().compareTo(o.getName());
}
return i;
}
}