Map集合
概述
Map<K,V>是一个接口,K为此映射所维护的键的类型
V为映射值的类型
public interface Map<K,V> 将键映射到值的对象。一个映射不能包含重复的键(K不允许重复);每个键最多只能映射到一个值。
Map接口定义了双列集合的规范,每次存储一对元素
Collections元素是孤立存在的,向集合中存储元素只能一个一个存。Map集合中每对元素都由键和值两部分组成(一个key,一个value),通过键可以找到对应的值。即键值对。
Map集合中k值不能重复,value可以重复
常用类
HashMap()集合
HashMap集合的特点:
- HashMap集合底层是哈希表:查询的速度特别的快
为数组+单向链表/红黑树(链表的长度超过8):提高查询的速度 - hashMap集合是一个无序的集合,存储元素和取出元素的顺序有可能不一致
java.util.LinkedHashMap<k, v>集合extends HashMap<k, v>集合
LinkedHashMap的特点: - LinkedHashMap集合底层是哈希表+链表(保证迭代的顺序)
- L inkedHashMap集合是-一个有序的集合,存储元素和取出元素的顺序是一致的
方法
public V put(K key. V value): 把指定的键与指定的值添加到Map集合中
返回值:v
存储键值对的时候,如果key值不重复,返回值v是null
存储键值对的时候。key重复,会使用新的value替换map
public V remove(Object key):把指定的键所对应的键值对元素在Map集合中删除,
返回被删除元素的值。
返回值:V
key存在,v返回被删的值
key不存在,v返回null
public V get(Object key):根据指定的键,在Nap集合中获取对应的值。
返回值:v
key存在,返回对应的value值
key不存在,近回null
public containsKey(Object key) 判断集合中是否包含指定的键。
包含返回true,不包含返回false
实例代码:
import java.util.Map;
import java.util.HashMap;
public class HashMapClass {
/**
* public V put(K key. V value):把指定的
* 键与指定的值添加到Map集合中
* 返回值:v
* 存储键值对的时候,如果key值不重复,返回值v是null
* 存储键值对的时候。key重复,会使用新的value替换map
* 中重复的value返回值为被天幻的value值
*/
public static void showput() {
//创建一个Map集合的对象,多态
Map<String,String> map = new HashMap<>();
String v1 = map.put("李晨","范冰冰1");
System.out.println("v1:" + v1);
String v2 = map.put("李晨","范冰冰");
System.out.println("v2:" + v2);
System.out.println(map);
map.put("1", "a");
map.put("2", "b");
map.put("3", "c");
System.out.println(map);
}
public static void showremove() {
//创建Map集合对象
Map<String,Integer> map = new HashMap<>();
map.put("赵丽颖",159);
map.put("哈哈哈", 160);
map.put("制造者", 161);
System.out.println(map);
Integer v1 = map.remove("赵丽颖");
System.out.println("v1:" + v1);
System.out.println(map);
Integer v2 = map.remove("赵");
System.out.println("v2:" + v2);
System.out.println(map);
}
public static void showget() {
//创建一个Map集合的对象,多态
Map<String,Integer> map = new HashMap<>();
map.put("迪丽热巴",159);
map.put("马尔巴哈", 160);
map.put("制造者", 161);
Integer v1 = map.get("迪丽热巴");
System.out.println("v1:" + v1);
Integer v2 = map.get("李白");
System.out.println("v2:"+ v2);
}
public static void showcontains() {
//创建一个Map集合的对象,多态
Map<String,Integer> map = new HashMap<>();
map.put("迪丽热巴",159);
map.put("马尔巴哈", 160);
map.put("制造者", 161);
boolean v1 = map.containsKey("迪丽热巴");
System.out.println("v1:" + v1);
boolean v2 = map.containsKey("李白");
System.out.println("v2:"+ v2);
}
public static void main(String[] args) {
showput();
System.out.println("==========================");
showremove();
System.out.println("==========================");
showget();
System.out.println("==========================");
showcontains();
}
}
输出结果:
遍历map集合
通过键找值的方式来遍历map集合。
Map集合中的方法:
Set keySet() 返回此映射中包含的键的Set 视图。
实现步骤:
1.使用Map集合中的方法keySet(),把Map集合所有的key取出来,存储到-个Set集合中
2.遍历set集合,获取Map集合中的每一个key
3.通过Map集合中的方法get(key),通过key找到value
实例代码:
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.Iterator;
public class KeySetClass {
public static void main(String[] args) {
Map<String,Integer> map = new HashMap<>();
map.put("赵丽颖",159);
map.put("哈哈哈", 160);
map.put("制造者", 161);
//使用Map中的keySet方法,将key值全部存储到set集合中
Set<String> set = map.keySet();
//使用迭代器遍历set中的key值,通过key找到值,输出
Iterator<String> it = set.iterator();
while(it.hasNext()) {
String key = it.next();
Integer value = map.get(key);
System.out.println(key + "=" + value);
}
System.out.println("=======================");
//使用增强for循环遍历
for(String key:set) {
Integer value = map.get(key);
System.out.println(key + "=" + value);
}
}
}
输出结果:
Map.Entry<K,V>
map接口中有一个内部接口Entry
作用:
当Map集合一创建,那么就会在Map集合中创建一个Entry对象,用来记录键与值。
图解,可用Entry对象遍历集合
(图片来此b站的黑马程序员网课)
使用Entry对象遍历
Mop集合中的方法:
Set<Map.Entry<K,V> entryset() 近回此映射中包含的映射关系的set视图。
实现步骤:,
1.使用Map集合中的方法entrySet(),把Map集合中多个Entry对象取出来,存储到一个Set集合中
2.遍历Set集合,获取每一个Entry对象
3.使用Entry对象中的方法getKey()和getValue()获取键与值
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.Iterator;
public class EntryMapClass {
public static void main(String[] args) {
Map<String,Integer> map = new HashMap<>();
map.put("赵丽颖",159);
map.put("哈哈哈", 160);
map.put("制造者", 161);
//使用entrySet()方法,把Map集合中多个Entry对象取出存储到Set集合中
Set<Map.Entry<String,Integer>> set = map.entrySet();
//遍历Set集合,获取每一个Entry对象
//使用迭代器遍历
Iterator<Map.Entry<String, Integer>> it = set.iterator();
while(it.hasNext()) {
Map.Entry<String, Integer> entry = it.next();
//使用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 = entry.getValue();
System.out.println(key + "=" + value);
}
}
}
输出结果