java-API之集合9——Map接口
Map特点
- 将键映射到值的对象。
- 一个映射不能包含重复的键(如果键重复,后面的值会覆盖前面的);每个键最多只能映射到一个值。
- Map 接口提供三种collection 视图,允许以键集、值集或键-值映射关系集的形式查看某个映射的内容。
参考源码:
/** * The default initial capacity - MUST be a power of two. */ static final int DEFAULT_INITIAL_CAPACITY = 1 << 4; // aka 16 /** * The load factor used when none specified in constructor. */ static final float DEFAULT_LOAD_FACTOR = 0.75f; /** * Constructs an empty <tt>HashMap</tt> with the default initial capacity * (16) and the default load factor (0.75). */ public HashMap() { this.loadFactor = DEFAULT_LOAD_FACTOR; // all other fields defaulted }
继承简易图
常用方法
- 添加元素
- 删除元素
- 获取元素
- V
clear()
从此映射中移除所有映射关系(可选操作)。
- V
- 其它
void clear()
从此映射中移除所有映射关系(可选操作)。boolean containsKey(Object key)
如果此映射包含指定键的映射关系,则返回 true。boolena containsKey(Object key)
如果此映射包含指定键的映射关系,则返回 true。boolean equals(Object o)
比较指定的对象与此映射是否相等。int hashCode()
返回此映射的哈希码值。boolean isEmpty()
如果此映射未包含键-值映射关系,则返回 true。int size()
返回此映射中的键-值映射关系数。Collection<V> values()
返回此映射中包含的值的Collection
视图。
返回此映射中包含的键的Set<K>
Set
视图。
返回此映射中包含的映射关系的Set<Map.Entry<K,V
>>
Set
视图。
测试
public class Test5_Map { public static void main(String[] args) { // 创建Map对象 // map里的元素是一对映射关系的数据,其中key和value可以泛型约束 Map<String,Integer> map = new HashMap<>(); map.put("3", 300); // 向map中添加数据时,需要同时指定Key和Value map.put("4", 400); map.put("1", 100); map.put("2", 200); // map数据无序 System.out.println(map); // {1=1, 2=2, 3=3, 4=4} map.put("1",100); System.out.println(map); // map中的key不可以重复,否则key对应的value值将会被覆盖 // map.clear(); // 从此映射中移除所有映射关系(可选操作)。 System.out.println(map.containsKey("1")); // true 如果此映射包含指定键的映射关系,则返回 true。 System.out.println(map.containsKey("100")); //false System.out.println(map.containsValue(2)); // true 如果此映射将一个或多个键映射到指定值,则返回 true。 System.out.println(map.containsValue(100)); // false System.out.println(map.entrySet()); // [1=100, 2=200, 3=300, 4=400] 返回此映射中包含的映射关系的 Set 视图。 System.out.println(map.hashCode()); // 1042 返回此映射的哈希码值。 System.out.println(map.get("3")); // 300 返回指定键所映射的值;如果此映射不包含该键的映射关系,则返回 null。 System.out.println(map.equals("110")); // false 比较指定的对象与此映射是否相等。 System.out.println(map.isEmpty()); // false 如果此映射未包含键-值映射关系,则返回 true。 System.out.println(map.remove("1")); // 100 如果存在一个键的映射关系,则将其从此映射中移除(可选操作)。 System.out.println(map); // {2=200, 3=300, 4=400} System.out.println(map.size()); // 3 返回此映射中的键-值映射关系数。 System.out.println(map.values()); // [[200, 300, 400] 返回此映射中包含的值的 Collection 视图。 } }
map中的几种迭代方式
public class Test6_Map { public static void main(String[] args) { Map<String,Integer> map = new HashMap<>(); map.put("3", 300); // 2、向map中添加数据时,需要同时指定Key和Value map.put("4", 400); map.put("1", 100); map.put("2", 200); System.out.println(map); // 第一种方式 // Set<k> keySet() // 把map中的key数据收集到set集合里 Set<String> set = map.keySet(); Iterator<String> it = set.iterator(); while(it.hasNext()) { Object key= it.next(); System.out.println("Key:" + key + ",Value:" + map.get(key)); } // 第二种方式 // Set<Map.Entry<K,V>> entrySet() --> 把map中的每条记录封装成Entry对象存入set Set<Entry<String,Integer>> entries = map.entrySet(); for(Entry<String,Integer> entry : entries ) { System.out.println("Key:" + entry.getKey() + ",Value:" + entry.getValue()); } // 第三种方式 Iterator it2 = entries.iterator(); while(it2.hasNext()) { Entry<String,Integer> entry = (Entry<String, Integer>) it2.next(); System.out.println("Key:" + entry.getKey() + ",Value:" + entry.getValue()); } } }