一、Map类
(一)Map类的介绍
Map 提供了一个更通用的元素存储方法。Map 集合类用于存储元素对(称作“键”和“值”),其中每个键映射到一个值。从概念上而言,您可以将 List 看作是具有数值键的 Map。
1.1:构造函数定义和方法
1. Map提供接口分别用于返回 键集、值集或键-值映射关系集。entrySet()用于返回键-值集的Set集合;keySet()用于返回键集的Set集合;values()用户返回值集的Collection集合,因为Map中不能包含重复的键;每个键最多只能映射到一个值。所以,键-值集、键集都是Set,值集时Collection。
2. Map提供了“键-值对”、“根据键获取值”、“删除键”、“获取容量大小”等方法。
构造函数定义
public interface Map<K,V> { }
方法
abstract void clear()
abstract boolean containsKey(Object key)
abstract boolean containsValue(Object value)
abstract Set<Entry<K, V>> entrySet()
abstract boolean equals(Object object)
abstract V get(Object key)
abstract int hashCode()
abstract boolean isEmpty()
abstract Set<K> keySet()
abstract V put(K key, V value)
abstract void putAll(Map<? extends K, ? extends V> map)
abstract V remove(Object key)
abstract int size()
abstract Collection<V> values()
1.2:Map的架构图
借鉴【野生开发者】的一篇文章:
Java集合框架——Map_java集合结构图-CSDN博客
如上图:
(01) Map 是映射接口,Map中存储的内容是键值对(key-value)。
(02) AbstractMap 是继承于Map的抽象类,它实现了Map中的大部分API。其它Map的实现类可以通过继承AbstractMap来减少重复编码。
(03) SortedMap 是继承于Map的接口。SortedMap中的内容是排序的键值对,排序的方法是通过比较器(Comparator)。
(04) NavigableMap 是继承于SortedMap的接口。相比于SortedMap,NavigableMap有一系列的导航方法;如"获取大于/等于某对象的键值对"、“获取小于/等于某对象的键值对”等等。
(05) TreeMap 继承于AbstractMap,且实现了NavigableMap接口;因此,TreeMap中的内容是“有序的键值对”!
(06) HashMap 继承于AbstractMap,但没实现NavigableMap接口;因此,HashMap的内容是“键值对,但不保证次序”!
(07) Hashtable 虽然不是继承于AbstractMap,但它继承于Dictionary(Dictionary也是键值对的接口),而且也实现Map接口;因此,Hashtable的内容也是“键值对,也不保证次序”。但和HashMap相比,Hashtable是线程安全的,而且它支持通过Enumeration去遍历。
(08) WeakHashMap 继承于AbstractMap。它和HashMap的键类型不同,WeakHashMap的键是“弱键”。
1.3:总结
1. Map 是一个键值对(key-value)映射接口。Map映射中不能包含重复的键;每个键最多只能映射到一个值。
2. Map 接口提供三种collection 视图,允许以键集、值集或键-值映射关系集的形式查看某个映射的内容。
3. Map 映射顺序。有些实现类,可以明确保证其顺序,如 TreeMap;另一些映射实现则不保证顺序,如 HashMap 类。
4. Map 的实现类应该提供2个“标准的”构造方法:第一个,void(无参数)构造方法,用于创建空映射;第二个,带有单个 Map 类型参数的构造方法,用于创建一个与其参数具有相同键-值映射关系的新映射。实际上,后一个构造方法允许用户复制任意映射,生成所需类的一个等价映射。尽管无法强制执行此建议(因为接口不能包含构造方法),但是 JDK 中所有通用的映射实现都遵从它。
(二)Map遍历的方法(3种)
2.1:用for循环遍历
推荐用第一种遍历方法,效率比较高。
for(Map.Entry<String, String> entry:map.entrySet()){
System.out.println(entry.getKey()+"--->"+entry.getValue());
}
2.2:用Iterator迭代遍历
Set set = map.entrySet();
Iterator i = set.iterator();
while(i.hasNext()){
Map.Entry<String, String> entry1=(Map.Entry<String, String>)i.next();
System.out.println(entry1.getKey()+"=="+entry1.getValue());
}
2.3:用keySet()迭代遍历
Iterator it=map.keySet().iterator();
while(it.hasNext()){
String key;
String value;
key=it.next().toString();
value=map.get(key);
System.out.println(key+"--"+value);
}
(三)Map遍历key的方法(4种)
4.1:keySet迭代遍历key
Iterator<String> iter = map.keySet().iterator();
while (iter.hasNext()) {
key = iter.next();
System.out.println("打印内容key:",key);
}
4.2:keySet用for循环遍历key
推荐用第二种遍历方式。
for (String key : map.keySet()) {
//key
System.out.println("打印内容key:",key);
}
4.3:entrySet迭代遍历key
Iterator<Entry<String, String>> iter = map.entrySet().iterator();
while (iter.hasNext()) {
key = iter.next().getKey();
System.out.println("打印内容key:",key);
}
4.4:entrySet用for循环遍历key
for (Entry<String, String> entry: map.entrySet()) {
key = entry.getKey();
System.out.println("打印内容key:",key);
}
(四)Map遍历value的方法(6种)
4.1:keySet迭代遍历value
Iterator<String> iter = map.keySet().iterator();
while (iter.hasNext()) {
value = map.get(iter.next());
}
4.2. keySet用for循环遍历value
for (String key : map.keySet()) {
value = map.get(key);
}
4.3:entrySet迭代遍历value
Iterator<Entry<String, String>> iter = map.entrySet().iterator();
while (iter.hasNext()) {
value = iter.next().getValue();
}
4.4:entrySet用for循环遍历value
for (Entry<String, String> entry: map.entrySet()) {
value = entry.getValue();
}
4.5:values迭代遍历value
Iterator<String> iter = map.values().iterator();
while (iter.hasNext()) {
value = iter.next();
}
4.6:values用for循环遍历value
for (String value : map.values()) {
System.out.println("打印内容value:",value);
}
(五)Map.put() 添加集合
Map存储元素使用put方法.
定义
put(K key, V value) 将指定的值与此映射中的指定键关联
putAll(Map<? extends K,? extends V> m) 从指定映射中将所有映射关系复制到此映射中
例子
/**
*
* Map集合添加
*
*/
public class MapDemo
{
public static void main(String[] args)
{
Map<String,Integer> m = new HashMap<String,Integer>();
m.put("zhangsan", 19);
m.put("lisi", 49);
m.put("wangwu", 19);
m.put("lisi",20);
m.put("hanmeimei", null);
System.out.println("输出集合内容:",m);
}
}
(六)Map.clear() 清除集合
Map移除所有元素使用clear方法.
定义
clear() 从此映射中移除所有映射关系
例子
/**
*
* Map集合删除
*
*/
public class MapDemo
{
public static void main(String[] args)
{
Map<String,Integer> m = new HashMap<String,Integer>();
m.put("zhangsan", 19);
m.put("lisi", 49);
m.put("wangwu", 19);
m.put("lisi",20);
m.put("hanmeimei", null);
System.out.println(m);
System.out.println(m.remove("wangwu"));
m.clear();
System.out.println(m);
}
}
(七)Map.remove() 移除集合
Map移除指定的元素使用remove方法.
定义
remove(Object key) 如果存在一个键的映射关系,则将其从此映射中移除
例子
/**
*
* Map移除集合
*
*/
public class MapDemo
{
public static void main(String[] args)
{
Map<String,Integer> m = new HashMap<String,Integer>();
m.put("zhangsan", 19);
m.put("lisi", 49);
m.put("wangwu", 19);
m.put("lisi",20);
m.put("hanmeimei", null);
System.out.println(m);
System.out.println(m.remove("wangwu"));
System.out.println(m);
}
}
(八)Map.containsValue() 是否包含value
Map判断是否包含指定的value使用containsValue方法.
定义
containsValue(Object value) 如果此映射将一个或多个键映射到指定值,则返回 true
例子
/**
*
* Map集合判断是否包含value
*
*/
public class MapDemo
{
public static void main(String[] args)
{
Map<String,Integer> m = new HashMap<String,Integer>();
m.put("zhangsan", 19);
m.put("lisi", 49);
m.put("wangwu", 19);
m.put("lisi",20);
m.put("hanmeimei", null);
System.out.println(m);
System.out.println(m.containsValue(20));
}
}
(九)Map.containsKey() 是否包含key
Map是否包含指定的key使用containsKey方法.
定义
containsKey(Object key) 如果此映射包含指定键的映射关系,则返回 true
例子
/**
*
* Map集合判断是否包含key
*
*/
public class MapDemo
{
public static void main(String[] args)
{
Map<String,Integer> m = new HashMap<String,Integer>();
m.put("zhangsan", 19);
m.put("lisi", 49);
m.put("wangwu", 19);
m.put("lisi",20);
m.put("hanmeimei", null);
System.out.println(m);
System.out.println(m.containsKey("lisi"));
}
}
(十)Map.isEmpty() 是否包含集合数据
Map判断是否包含集合使用isEmpty方法.
定义
isEmpty() 如果此映射未包含键-值映射关系,则返回 true
例子
/**
*
* Map判断是否包含集合数据
*
*/
public class MapDemo
{
public static void main(String[] args)
{
Map<String,Integer> m = new HashMap<String,Integer>();
m.put("zhangsan", 19);
m.put("lisi", 49);
m.put("wangwu", 19);
m.put("lisi",20);
m.put("hanmeimei", null);
System.out.println(m);
System.out.println(m.isEmpty());
}
}
(十一)Map.get() 获取集合value数据
Map获取结合的value数据使用get方法.
定义
get(Object key) 返回指定键所映射的值;如果此映射不包含该键的映射关系,则返回 null
例子
/**
*
* Map获取集合value数据
*
*/
public class MapDemo
{
public static void main(String[] args)
{
Map<String,Integer> m = new HashMap<String,Integer>();
m.put("zhangsan", 19);
m.put("lisi", 49);
m.put("wangwu", 19);
m.put("lisi",20);
m.put("hanmeimei", null);
System.out.println(m);
System.out.println(m.get("lisi"));
}
}
(十二)Map.size() 获取集合总条数
Map获取集合总条数使用remove方法.
定义
size() 返回此映射中的键-值映射关系数。
例子
/**
*
* Map获取集合总条数
*
*/
public class MapDemo
{
public static void main(String[] args)
{
Map<String,Integer> m = new HashMap<String,Integer>();
m.put("zhangsan", 19);
m.put("lisi", 49);
m.put("wangwu", 19);
m.put("lisi",20);
m.put("hanmeimei", null);
System.out.println(m);
System.out.println(m.size());
}
}
(十三)Map.values() 获取集合所有value值
Map获取集合所有value值使用values方法.
定义
values() 返回此映射中包含的值的 collection 视图
例子
/**
*
* Map获取集合所有value值
*
*/
public class MapDemo
{
public static void main(String[] args)
{
Map<String,Integer> m = new HashMap<String,Integer>();
m.put("zhangsan", 19);
m.put("lisi", 49);
m.put("wangwu", 19);
m.put("lisi",20);
m.put("hanmeimei", null);
System.out.println(m);
System.out.println(m.values());
}
}
(十四)Map.entrySet() 集合映射成set视图
Map集合映射成set视图使用entrySet方法.
定义
entrySet() 返回此映射中包含的映射关系的 set 视图。
例子
/**
*
* Map集合映射成set视图
*
*/
public class MapDemo
{
public static void main(String[] args)
{
Map<String,Integer> m = new HashMap<String,Integer>();
m.put("zhangsan", 19);
m.put("lisi", 49);
m.put("wangwu", 19);
m.put("lisi",20);
m.put("hanmeimei", null);
System.out.println(m);
System.out.println(m.entrySet());
}
}
(十五)Map.keySet() 集合映射成key视图
Map集合映射成key视图使用keySet方法.
定义
keySet() 返回此映射中包含的映射关系的 key 视图。
例子
/**
*
* Map集合映射成key视图
*
*/
public class MapDemo
{
public static void main(String[] args)
{
Map<String,Integer> m = new HashMap<String,Integer>();
m.put("zhangsan", 19);
m.put("lisi", 49);
m.put("wangwu", 19);
m.put("lisi",20);
m.put("hanmeimei", null);
System.out.println(m);
System.out.println(m.keySet());
}
}