目录
1.Map集合概述和使用
Map集合是一种双列集合
每个元素包含两个数据
Map集合的每个元素的格式: key=value(键值对元素)。
Map集合也被称为“键值对集合”
2.Map集合整体格式:
collection集合的格式:[元素1,元素2,元素3..]
Map集合的完整格式: {key1=value1 , key2=value2 , key3=value3 , ..}
1.Map集合是什么?使用场景是什么样的?
Map集合是键值对集合;
Map集合非常适合做类购物车种类的业务场景
3.Map集合体系特点
Map集合的特点都是由键决定的。
Map集合的键是无序,不重复的,无索引的,值不做要求(可以重复)。
Map集合后面重复的键对应的值会覆盖前面重复键的值。
Map集合的键值对都可以为null。
4.Map集合实现类特点
HashMap:元素按照键是无序,不重复,无索引,值不做要求。(与Map体系一致)
LinkedHashMap:元素按照键是有序,不重复,无索引,值不做要求。
TreeMap:元素按照建是排序,不重复,无索引的,值不做要求。
5.Map集合
Map是双列集合的祖宗接口,它的功能是全部双列集合都可以继承使用的。
API:
方法名称 | 说明 |
v put(K key,v value) | 添加元素 |
v remove( Object key) | 根据键删除键值对元素 |
void clear() | 移除所有的键值对元素 |
boolean containsKey(object key) | 判断集合是否包含指定的键 |
boolean containsvalue(0bject value) | 判断集合是否包含指定的值 |
boolean isEmpty() | 判断集合是否为空 |
int size(() | 集合的长度,也就是集合中键值对的个数 |
6.Map集合的遍历
6.1方式一:键找值
先获取Map集合的全部键的Set集合。
遍历键的Set集合,然后通过键提取对应值。
API:
方法名称 | 说明 |
Set<K> keySet() | 获取所有键的集合 |
v get(Object key) | 根据键获取值 |
6.2方式二:键值对
先把Map集合转换成Set集合,Set集合中每个元素都是键值对实体类型了。
遍历Set集合,然后提取键以及提取值。
API:
方法名称 | 说明 |
Set<Map.Entry<KV> > entrySet() | 获取所有键值对对象的集合 |
K getKey() | 获取键 |
V getValue() | 获取值 |
6.3方式三:Lambda
得益于JDK 8开始的新技术Lambda表达式,提供了一种更简单、更直接的遍历集合的方式。
Map结合Lambda遍历的API
方法名称 | 说明 |
default void forEach(BiConsumer<? super K,? super V> action) | 结合lambda遍历Map集合 |
7.案例
package d5.map;
import static java.lang.System.out;
import java.util.Collection;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;
/**
* HashMap 无序 不重复 无索引 值不做要求
* LinkedHashMap 有序 不重复 无索引 值不做要求
* @author Acloasia
*
*/
public class Mapdome1 {
public static void main(String[] args) {
Map<String,Integer> maps = new LinkedHashMap<>();
//Map<String,Integer> maps = new HashMap<>();
maps.put("小米",5);
maps.put("OPPO", 1);
maps.put("hauwei", 2);
maps.put("OPPO", 100);
maps.put(null, null);
System.out.println(maps);
//判断集合是否为空,为空 返回true ,反之false
System.out.println(maps.isEmpty());
//1.清空集合
//maps.clear();
System.out.println(maps);
//2.判断集合是否为空,为空返回true ,反之返回false
System.out.println(maps.isEmpty());
//3.根据键获取对应值
Integer key = maps.get("小米");
Integer key1 = maps.get("小米1");
Integer key2 = maps.get("OPPO");
System.out.println(key);
System.out.println(key1); //没有获取到相应的键 则为null
System.out.println(key2);
//4.判断集合中是否包含莫个键,包含返回true。反之返回false
System.out.println(maps.containsKey("小米")); //true
System.out.println(maps.containsKey("OPPO")); //true
System.out.println(maps.containsKey("VIVO")); //false
//5.判断集合是否包含某个值
System.out.println(maps.containsValue(5)); //true
out.println(maps.containsValue(1)); //false
System.out.println(maps.containsValue(100)); //true
//6.判断全部值的集合:Collection<V> values();
Collection<Integer> values = maps.values();
System.out.println(values);
//7.集合的大小
System.out.println(maps.size());
//8.Map合并
Map<String,Integer> map1 = new HashMap<>();
map1.put("map1的小米",1111);
map1.put("map1的OPPO",2222);
map1.put("map1的VIVO",3333);
Map<String,Integer> map2 = new HashMap<>();
map2.put("map2的小米",4444);
map2.put("map2的OPPO",5555);
map2.put("map2的VIVO",6666);
map1.putAll(map2);
System.out.println(map1);
System.out.println(map2);
}
}