将键映射到值的对象,一个映射不能包含重复的键
java.util.Map<k,v>
map集合的特点:
1.Map集合是一个双列集合,一个元素包含两个值(一个key,一个value)
2.Map集合中的元素,key和value的数据类型可以相同,也可以不同
3.Map集合中的元素,key是不允许重复的,value是可以重复的
4.Map集合中的元素,key和valu是一一对应的
常用子类:
HashMap集合的特点:
1.HashMap集合底层是哈希表:查询的速度特别快
2.HashMap集合是一个无序的集合,存储元素和取出元素的顺序有可能不一致
java.util.LinkedHashMap<k,v>集合extends HashMap<k,v>集合
LinkedHashMap的特点:
1.LinkedHashMap集合底层是哈希表+链表(保证迭代的顺序)
2.LinkedHashMap集合是一个有序的集合,存储元素和取出元素的顺序是一致的
添加元素:collections用add,map用put(因为是两个值)
方法:put,remove,get,containsKey
/*
boolean containsKey(Object key)判断集合中是否包含指定的键
包含返回true,不包含返回false
*/
private static void shot04() {
//创建Map集合对象
Map<String,Integer> map=new HashMap<>();
map.put("mike",19);
map.put("john",20);
map.put("tom",17);
boolean b1=map.containsKey("mike");
System.out.println(b1);//true
boolean b2=map.containsKey("mke");
System.out.println(b2);//false
}
/*
public V get(Object key)根据指定的键,在Map集合中获取对应的值
*/
private static void show03() {
//创建Map集合对象
Map<String,Integer> map=new HashMap<>();
map.put("mike",19);
map.put("john",20);
map.put("tom",17);
Integer v1=map.get("mike");
System.out.println(v1);//返回value19
Integer v2=map.get("jack");
System.out.println(v2);//返回null
}
/*
public V remove(Object key):把指定的键所对应的键值对元素在Map集合中删除,返回被删除元素值
返回值:v
key存在:返回被删除的值
key不存在,v返回空
*/
private static void show02() {
//创建Map集合对象
Map<String,Integer> map=new HashMap<>();
map.put("mike",19);
map.put("john",20);
map.put("tom",17);
System.out.println(map);//{tom=17, mike=19, john=20}
Integer v1=map.remove("mike");
System.out.println(map);//{tom=17, john=20}
Integer v2=map.remove("jack");//用int接,会自动拆箱,但会出现空指针异常
System.out.println(v2);//没有这个value,v2=null
}
/*
public V put(K key,V value):把指定的键与指定的值添加到Map集合中
返回值:v
存储键值对的时候,key不重复,返回值v是null
存储键值对的时候,key重复,会使用新的value替换map中重复的value,返回被替换的value值
*/
private static void show01() {
//创建Map集合对象,多态
Map<String,String> map=new HashMap<>();
String v1=map.put("mike","john1");
System.out.println(v1);//没有返回值,是null
String v2=map.put("mike","john2");
System.out.println(v2);//返回被替换的值john1
System.out.println(map);//{mike=john2},不是地址说明重写了tostring
map.put("A","a");
map.put("B","b");
map.put("C","b");//k不能重复,v可以重复
System.out.println(map);//{A=a, B=b, C=b, mike=john2}
}
键找值的方式遍历
Map集合的第一种遍历方式:通过键找值的方式
Map集合中的方法:
Set keySet() 返回此映射中包含的键的Set视图。
实现步骤:
1.使用Map集合中的方法keySet(),把Map集合所有的key取出来,存储到一个Set集合中
2.遍历set集合,获取Map集合中的每一个key
3.通过Map集合中的方法get(key),通过key找到value
public static void main(String[] args) {
//创建Map集合对象
Map<String,Integer> map=new HashMap<>();
map.put("mike",19);
map.put("john",20);
map.put("tom",17);
//1.使用Map集合中的方法keySet(),把Map集合所有的key取出来,存储到一个Set集合中
Set<String> set=map.keySet();
//2.遍历set集合,获取Map集合中的每一个key
//使用迭代器遍历Set集合
Iterator<String> it=set.iterator();
System.out.println("使用迭代器");
while(it.hasNext()){
String key=it.next();
//3.通过Map集合中的方法get(key),通过key找到value
Integer value=map.get(key);
System.out.println(key+"="+value);
}
System.out.println("使用增强for遍历set集合");
for(String key:set){
Integer value=map.get(key);
System.out.println(key+"="+value);
}
}
结果:
使用迭代器
tom=17
mike=19
john=20
使用增强for遍历set集合
tom=17
mike=19
john=20
Entry键值对对象
Map.Entry<K,V>:在Map接口中有一个内部接口Entry
作用:当Map集合一创建,那么就会在Map集合中创建一个Entry对象,用来记录与值(键值对对象,键和值的映射关系)
Map集合遍历的第二种方式:使用Entry对象遍历
Map集合中的方法:
Set<Map,Entry<K,V>> entrySet()返回此映射中包含的映射关系的Set视图
实现步骤:
1.使用Map集合中的方法entrySet(),把Map集合中多个Entry对象取出来,存到一个Set集合中
2.遍历Set集合,获取每一个Entry对象
3.使用Entry对象中的方法getKey()和getValue()获取键与值
public static void main(String[] args) {
//创建Map集合对象
Map<String,Integer> map=new HashMap<>();
map.put("mike",19);
map.put("john",20);
map.put("tom",17);
//1.使用Map集合中的方法entrySet(),把Map集合中多个Entry对象取出来,存到一个Set集合中
Set<Map.Entry<String,Integer>> set=map.entrySet();
//2.遍历Set集合,获取每一个Entry对象
//使用迭代器遍历Set集合
Iterator<Map.Entry<String,Integer>> it=set.iterator();
System.out.println("使用迭代器");
while(it.hasNext()){
Map.Entry<String,Integer> entry=it.next();
//3.使用Entry对象中的方法getKey()和getValue()获取键与值
String key=entry.getKey();
Integer value=entry.getValue();
System.out.println(key+"="+value);
}
System.out.println("使用增强for");
for(Map.Entry<String,Integer> entry:set){
String key=entry.getKey();
Integer value=map.get(key);
System.out.println(key+"="+value);
}
HashMap存储自定义类型键值
Map集合保证key是唯一的
作为key的元素,必须重写hashCode方法和equals方法以保证key唯一
public static void main(String[] args) {
//show01();
show02();
}
/*
HashMap存储自定义类型键值
key:Person类型
Person类必须重写hashCode方法和equals方法以保证key唯一
value:String类型
可以重复
*/
private static void show02() {
//创建HashMap集合
HashMap<Person,String> map=new HashMap<>();
//往集合中添加元素
map.put(new Person("mike",19),"北京");
map.put(new Person("john",19),"上海");
map.put(new Person("tom",19),"广州");
map.put(new Person("mike",19),"北京");
//使用entrySet和增强for遍历
Set<Map.Entry<Person,String>> set=map.entrySet();
for (Map.Entry<Person,String>entry:set){
Person key=entry.getKey();
String value=entry.getValue();
System.out.println(key+"-->"+value);
}
}
/*
HashMap存储自定义类型键值
key:String类型
String类型重写hashCode方法和equals方法可以保证key唯一
value:Person类型
value可以重复(同名同年龄的人视为同一个)
*/
private static void show01(){
//创建HashMap集合
HashMap<String,Person> map=new HashMap<>();
//往集合中添加元素
map.put("北京",new Person("mike",19));
map.put("上海",new Person("john",19));
map.put("广州",new Person("tom",20));
map.put("北京",new Person("jerry",19));
//使用keySet+增强for遍历Map集合
Set<String> set=map.keySet();
for (String key : set) {
Person value=map.get(key);
System.out.println(key+"-->"+value);//没有jerry,key一样认为相同
}
}
LinkedHashMap
java.util.LinkedHashMap<K,V> entends HashMap<K,V>
Map 接口的哈希表和链接列表实现,具有可预知的迭代顺序
底层原理:
哈希表+链表(记录元素的顺序)
public static void main(String[] args) {
HashMap<String,String> map=new HashMap<>();
map.put("a","a");
map.put("c","c");
map.put("b","b");
map.put("a","d");
System.out.println(map);//{a=d, b=b, c=c}
//key不允许重复,无序的集合,存取顺序可能不一致
LinkedHashMap<String,String> linked=new LinkedHashMap<>();
linked.put("a","a");
linked.put("c","c");
linked.put("b","b");
linked.put("a","d");
System.out.println(linked);//{a=d, c=c, b=b}
//key不允许重复,有序的集合
}
Hashtable
java.util.Hashtable<K,V>集合
Hashtable:底层也是一个哈希表,是一个线程安全的集合,是单线程集合,速度慢
HashMap:底层是一个哈希表,是一个线程不安全的集合,是多线程的集合,速度快
HashMap集合(和其他所有集合):可以存储null值,null键
Hashtable集合:不能存储null值,null键
Hashtable和Vector集合一样,在jkd1.2之后被更先进的集合(HashMap,ArrayList)取代了
Hashtable的子类Properties仍在使用
Properties集合是一个唯一和IO流相结合的集合
public static void main(String[] args) {
HashMap<String,String> map=new HashMap();
map.put(null,"a");
map.put("V",null);
map.put(null,null);
System.out.println(map);//{null=null, V=null}
//允许空值空键,且会被替代
Hashtable<String,String> table=new Hashtable<>();
table.put(null,"a");//空指针异常,不允许存储空值空键
table.put("b",null);
}
小实例:计算一个字符串中每个字符出现次数
分析:1.使用Scanner获取用户输入的字符串
2.创建Map集合,key是字符串中的字符,value是字符的个数
3.遍历字符串,获取每一个字符
4.使用获取到的字符,去Map集合判断key是否存在
key存在:
通过字符(key),获取value(字符个数)
value++
put(key,value)把新的value存储到Map集合中
key不存在:
put(key,1)
5.遍历Map集合,输出结果
public static void main(String[] args) {
//1.使用Scanner获取用户输入的字符串
Scanner sc=new Scanner(System.in);
String str=sc.next();
//2.创建Map集合,key是字符串中的字符,value是字符的个数
HashMap<Character,Integer> map=new HashMap<>();
//3.遍历字符串获取每一个字符
for(char c:str.toCharArray()){
//4.使用获取到的字符去Map判断key是否存在
if(map.containsKey(c)){
//存在
Integer value=map.get(c);
map.put(c,value+1);
}
else {
//不存在
map.put(c,1);
}
}
for(Character c:map.keySet()){
Integer value=map.get(c);
System.out.println(c+"-->"+value);
}
}
of方法:
JDK9的新特性:
List接口,Set接口,Map接口:里边增加了一个静态的方法of,可以给集合一次性添加多个元素
static List of (E…elements)
使用前提:
当集合中存储的元素的个数已经确定了,不再改变时使用
注意:
1.of方法只适用于List接口,Set接口,Map接口,不适用于接口的实现类
2.of方法的返回值是一个不能改变的集合,集合不能再使用add,put方法添加元素,会抛出异常
3.Set接口和Map接口在调用of方法的时候,不能有重复的元素,否则会抛出异常
public static void main(String[] args) {
List<String> list=List.of("a","b","c","d","e");
System.out.println(list);
//list.add("w");//UnsupportedOperationException 不支持操作异常
Set<String> set=Set.of("a","b","c","d","e");
//Set<String> set=Set.of("a","b","a","d","e");//duplicate element: a:有重复元素,非法参数异常
System.out.println(set);
//set.add("w");UnsupportedOperationException 不支持操作异常
Map<String,Integer> map=Map.of("mike",19,"john",20,"jane",30);
//Map<String,Integer> map=Map.of("mike",19,"john",20,"jane",30,"jane",10);//duplicate key: jane
map.put("ll",29);//UnsupportedOperationException
System.out.println(map);
}