- Map与Collection并列存在。用于保存具有映射关系的数据:Key-Value
- Map中的key和value可以使任何引用类型的数据,会封装到HashMap$Node对象中
- Map中的key不允许重复,愿意和HashSet一样
- Map中的value可以重复
- Map的key可以为null,value也可以为null,注意key的null只能有一个
- 常用String类作为Map的key
- key和value之间存在单向一对一关系,即通过指定的key总能找到对应的value
- put相同key的元素,将会发生替换
-
Map存放的数据key-value,放在一个HashMap$Node中(实现了Entry接口)
static class Node
implements Map.Entry { final int hash; final K key; V value; Node next; Node(int hash, K key, V value, Node next) { this.hash = hash; this.key = key; this.value = value; this.next = next; } public final K getKey() { return key; } public final V getValue() { return value; } public final String toString() { return key + "=" + value; } public final int hashCode() { return Objects.hashCode(key) ^ Objects.hashCode(value); } public final V setValue(V newValue) { V oldValue = value; value = newValue; return oldValue; } public final boolean equals(Object o) { if (o == this) return true; if (o instanceof Map.Entry) { Map.Entry,?> e = (Map.Entry,?>)o; if (Objects.equals(key, e.getKey()) && Objects.equals(value, e.getValue())) return true; } return false; } } -
entrySet()方法获取的Set
>中的Map.Entryj接口,直接引用Node中的数据,并没有复制(所以修改会影响原数据) interface Entry
{ K getKey(); V getValue(); V setValue(V value); int hashCode(); public static , V> Comparator > comparingByKey() { return (Comparator > & Serializable) (c1, c2) -> c1.getKey().compareTo(c2.getKey()); } public static > Comparator > comparingByValue() { return (Comparator > & Serializable) (c1, c2) -> c1.getValue().compareTo(c2.getValue()); } public static Comparator > comparingByKey(Comparator super K> cmp) { Objects.requireNonNull(cmp); return (Comparator > & Serializable) (c1, c2) -> cmp.compare(c1.getKey(), c2.getKey()); } public static Comparator > comparingByValue(Comparator super V> cmp) { Objects.requireNonNull(cmp); return (Comparator > & Serializable) (c1, c2) -> cmp.compare(c1.getValue(), c2.getValue()); } } -
keySet()一样返回一个内部类(实现了Set),直接引用原Node中的key
final class KeySet extends AbstractSet
{ public final int size() { return size; } public final void clear() { HashMap.this.clear(); } public final Iterator iterator() { return new KeyIterator(); } public final boolean contains(Object o) { return containsKey(o); } public final boolean remove(Object key) { return removeNode(hash(key), key, null, false, true) != null; } public final Spliterator spliterator() { return new KeySpliterator<>(HashMap.this, 0, -1, 0, 0); } public final void forEach(Consumer super K> action) { Node [] tab; if (action == null) throw new NullPointerException(); if (size > 0 && (tab = table) != null) { int mc = modCount; for (int i = 0; i < tab.length; ++i) { for (Node e = tab[i]; e != null; e = e.next) action.accept(e.key); } if (modCount != mc) throw new ConcurrentModificationException(); } } } -
values()也是返回一个内部类(实现了Collection接口),直接引用Node中的value
final class Values extends AbstractCollection
{ public final int size() { return size; } public final void clear() { HashMap.this.clear(); } public final Iterator iterator() { return new ValueIterator(); } public final boolean contains(Object o) { return containsValue(o); } public final Spliterator spliterator() { return new ValueSpliterator<>(HashMap.this, 0, -1, 0, 0); } public final void forEach(Consumer super V> action) { Node [] tab; if (action == null) throw new NullPointerException(); if (size > 0 && (tab = table) != null) { int mc = modCount; for (int i = 0; i < tab.length; ++i) { for (Node e = tab[i]; e != null; e = e.next) action.accept(e.value); } if (modCount != mc) throw new ConcurrentModificationException(); } } }
| void | clear() 从该Map中删除所有的映射(可选操作)。 |
|---|---|
| default V | compute(K key, BiFunction super K,? super V,? extends V> remappingFunction) 尝试计算指定键的映射及其当前映射的值(如果没有当前映射, null )。 |
| default V | computeIfAbsent(K key, Function super K,? extends V> mappingFunction) 如果指定的键尚未与值相关联(或映射到 null ),则尝试使用给定的映射函数计算其值,并将其输入到此映射中,除非 null 。 |
| default V | computeIfPresent(K key, BiFunction super K,? super V,? extends V> remappingFunction) 如果指定的密钥的值存在且非空,则尝试计算给定密钥及其当前映射值的新映射。 |
| boolean | containsKey(Object key) 如果此映射包含指定键的映射,则返回 true 。 |
| boolean | containsValue(Object value) 如果此Map将一个或多个键映射到指定的值,则返回 true 。 |
| Set | entrySet() 返回此Map中包含的映射的Set视图。 |
| boolean | equals(Object o) 将指定的对象与此映射进行比较以获得相等性。 |
| default void | forEach(BiConsumer super K,? super V> action) 对此映射中的每个条目执行给定的操作,直到所有条目都被处理或操作引发异常。 |
| V | get(Object key) 返回到指定键所映射的值,或 null如果此映射包含该键的映射。 |
| default V | getOrDefault(Object key, V defaultValue) 返回到指定键所映射的值,或 defaultValue如果此映射包含该键的映射。 |
| int | hashCode() 返回此Map的哈希码值。 |
| boolean | isEmpty() 如果此Map不包含键值映射,则返回 true 。 |
| Set | keySet() 返回此Map中包含的键的Set视图。 |
| default V | merge(K key, V value, BiFunction super V,? super V,? extends V> remappingFunction) 如果指定的键尚未与值相关联或与null相关联,则将其与给定的非空值相关联。 |
| V | put(K key, V value) 将指定的值与该映射中的指定键相关联(可选操作)。 |
| void | putAll(Map extends K,? extends V> m) 将指定Map的所有映射复制到此映射(可选操作)。 |
| default V | putIfAbsent(K key, V value) 如果指定的键尚未与某个值相关联(或映射到 null )将其与给定值相关联并返回 null ,否则返回当前值。 |
| V | remove(Object key) 如果存在(从可选的操作),从该Map中删除一个键的映射。 |
| default boolean | remove(Object key, Object value) 仅当指定的密钥当前映射到指定的值时删除该条目。 |
| default V | replace(K key, V value) 只有当目标映射到某个值时,才能替换指定键的条目。 |
| default boolean | replace(K key, V oldValue, V newValue) 仅当当前映射到指定的值时,才能替换指定键的条目。 |
| default void | replaceAll(BiFunction super K,? super V,? extends V> function) 将每个条目的值替换为对该条目调用给定函数的结果,直到所有条目都被处理或该函数抛出异常。 |
| int | size() 返回此Map中键值映射的数量。 |
| Collection | values() 返回此Map中包含的值的Collection视图 |
-
取出所有key,再通过key取出对应value
Set keySet = map.keySet(); //增强for for(Object key:keySet){ System.out.println(key + "=" + map.get(key)); } //迭代器 Iterator iterator = keySet.iterator(); while(iterator.hasNext()){ Object key = iterator.next(); System.out.println(key + "=" + map.get(key)); } -
把所有的values取出
Collection values = map.values();
//增强for
for(Object value:values){
System.out.println(value);
}
//迭代器
Iterator iterator = keyset.iterator();
while(iterator.hasNext()){
Object value = iterator.next();
System.out.println(value);
}
-
通过EntrySet来获取k-v
Set entrySet = map.entrySet(); //增强for for(Object entry:entrySet){ //将entry转为Map.Entry Map.Entry m = (Map.Entry) entry; System.out.println(m.getKey() + "=" + m.getValue(key)); } //迭代器 Iterator iterator = entrySet.iterator(); while(iterator.hasNext()){ Object entry = iterator.next();//HashMap&Node implement Map.Entry Map.Entry m = (Map.Entry) entry; System.out.println(m.getKey() + "=" + m.getValue(key)); }



