目录
存值特点
数据结构:
数组+单项链表+红黑树
图解:
变量
构造函数
public HashMap()
public HashMap(int initialCapacity):自定义数组长度
public HashMap(int initialCapacity, float loadFactor):自定义数组长度和家在因子
public HashMap(Map m):通过map初始化
public void clear():数组对应的值置空,即清空map
public Object clone():克隆map
public V compute(K key, BiFunction remappingFunction):对key在map中的值进行操作
public V computeIfAbsent(K key, Function mappingFunction):不存在加添加
public V computeIfPresent(K key,BiFunction remappingFunction)只对已经存在key的进行操作,其他不操作
public Set> entrySet():获取键值对Entry的Set集合
public void forEach(BiConsumer action):遍历
public V getOrDefault(Object key, V defaultValue):key存在返回value,key不存在返回defaultValue
public boolean isEmpty():判断集合是不是空
public Set keySet():获取key的set集合
public V merge(K key, V value,BiFunction remappingFunction);key存在修改,不存在新增
public void putAll(Map m):putmap
public V putIfAbsent(K key, V value):key不存在就添加
public boolean remove(Object key, Object value):对应的key、value存在就删除
public V replace(K key, V value):替换可以对应的value的值
public void replaceAll(BiFunction function):根据自定义规则修改元素
public int size():map的长度
public Collection values():获取值得集合
存值特点
key和value都可以为空
数据结构:
数组+单项链表+红黑树
链表长度>=8:链表变为红黑树
红黑树长度<=6:红黑树变为链表
链表节点
static class Node<K,V> implements Map.Entry<K,V> {
final int hash;//hash值
final K key;//可以值
V value;//值
Node<K,V> next;//指向下一个链表节点
数组
transient Node<K,V>[] table;
图解:
变量
static final int DEFAULT_INITIAL_CAPACITY = 1 << 4; | 默认数组初始容量-必须是2的幂 |
static final float DEFAULT_LOAD_FACTOR = 0.75f; | 在构造函数中未指定时使用的加载因子。 |
transient Set<Map.Entry<K,V>> entrySet; | 保存缓存entrySet ()。HashMap中value的集合 |
transient int size; | map包含元素容量 |
final float loadFactor; | 哈希表的装载因子 |
static final int MAXIMUM_CAPACITY = 1 << 30; | 最大数据容量 |
static final int MIN_TREEIFY_CAPACITY = 64; | 树形结构最小容量为64 |
transient int modCount; | 记录被改变的次数.主要用于iterators 的快速失败判断. |
transient Node<K,V>[] table; | 链表数组 |
int threshold; | 调整大小的下一个大小值(容量*加载因子)。 |
static final int TREEIFY_THRESHOLD = 8; | 链表>=8,转变为红黑树 |
static final int UNTREEIFY_THRESHOLD = 6; | 红黑树<=6,转变成链表 |
构造函数
public HashMap()
默认加载因子:0.75f
public HashMap(int initialCapacity):自定义数组长度
输入的数字会计算为最接近2的幂数
1 ->1 10->16 17->32 31->32 33->64
public HashMap(int initialCapacity, float loadFactor):自定义数组长度和家在因子
public HashMap(Map<? extends K, ? extends V> m):通过map初始化
public class Test {
public static void main(String[] args) {
Map<Integer,String> map = new HashMap<>(4);
map.put(1,"1");
map.put(2,"1");
map.put(3,"1");
map.put(4,"1");
Map<Integer,String> map2 = new HashMap<>(map);
System.out.println(map2);
}
}
结果:
{1=1, 2=1, 3=1, 4=1}
public void clear():数组对应的值置空,即清空map
public class Test {
public static void main(String[] args) {
Map<Integer,String> map = new HashMap<>(4);
map.put(1,"1");
map.put(2,"1");
map.put(3,"1");
map.put(4,"1");
map.clear();
System.out.println(map);
}
}
结果
{}
public Object clone():克隆map
注意:要用全名定义HashMap<Integer,String> map = new HashMap<>(4);,不能写成Map<Integer,String> map = new HashMap<>(4);
public class Test {
public static void main(String[] args) {
HashMap<Integer,String> map = new HashMap<>(4);
map.put(1,"1");
map.put(2,"1");
map.put(3,"1");
map.put(4,"1");
HashMap<Integer,String> map2 = (HashMap<Integer, String>) map.clone();
System.out.println(map2);
map2.remove(4);
System.out.println(map);
System.out.println(map2);
}
}
结果:对克隆的修改,不会影响原来的map
{1=1, 2=1, 3=1, 4=1}
{1=1, 2=1, 3=1, 4=1}
{1=1, 2=1, 3=1}
public V compute(K key, BiFunction<? super K, ? super V, ? extends V> remappingFunction):对key在map中的值进行操作
修改key=3,的value的值
public class Test {
public static void main(String[] args) {
HashMap<Integer,String> map = new HashMap<>(4);
map.put(1,"1");
map.put(2,"2");
map.put(3,"3");
map.put(4,"2");
String test = map.compute(3, (k,v) -> v+1 );
map.compute(5,(k,v)->{
return "test";
});
map.compute(2,(k,v)->{
return "test";
});
System.out.println(test);
System.out.println(map);
}
}
结果:1、对应的key存在,修改key对应value的值,2、如果对应的key不存在,新增一个key、value
31
{1=1, 2=test, 3=31, 4=2, 5=test}
public V computeIfAbsent(K key, Function<? super K, ? extends V> mappingFunction):不存在加添加
public class Test {
public static void main(String[] args) {
HashMap<Integer,String> map = new HashMap<>(4);
map.put(1,"1");
map.put(2,"2");
map.put(3,"3");
map.put(4,"2");
String test = map.computeIfAbsent(3, key -> "test" );
String test2 = map.computeIfAbsent(5, key -> "test" );
System.out.println(test);
System.out.println(test2);
System.out.println(map);
}
}
结果:1、存在直接返回;2、key不存在就添加一个值
3
test
{1=1, 2=2, 3=3, 4=2, 5=test}
public V computeIfPresent(K key,BiFunction<? super K, ? super V, ? extends V> remappingFunction)只对已经存在key的进行操作,其他不操作
public class Test {
public static void main(String[] args) {
HashMap<Integer,String> map = new HashMap<>(4);
map.put(1,"1");
map.put(2,"2");
map.put(3,"3");
map.put(4,"2");
String test = map.computeIfPresent(3, (key,value) -> "test" );
String test2 = map.computeIfPresent(5, (key,value) -> "test" );
System.out.println(test);
System.out.println(test2);
System.out.println(map);
}
}
结果:1、对key存在的进行操作;2、对key不存在的不作处理
test
null
{1=1, 2=2, 3=test, 4=2}
public boolean containsKey(Object key),containsValue(Object value):包含key、包含值
public class Test {
public static void main(String[] args) {
HashMap<Integer,String> map = new HashMap<>(4);
map.put(1,"1");
map.put(2,"2");
map.put(3,"3");
map.put(4,"2");
String test = map.computeIfPresent(3, (key,value) -> "test" );
String test2 = map.computeIfPresent(5, (key,value) -> "test" );
System.out.println(map.containsKey(1));
System.out.println(map.containsKey(5));
System.out.println(map.containsValue("1"));
System.out.println(map.containsValue("5"));
}
}
结果:
true
false
true
false
public Set<Map.Entry<K,V>> entrySet():获取键值对Entry的Set集合
public class Test {
public static void main(String[] args) {
HashMap<Integer,String> map = new HashMap<>(4);
map.put(1,"1");
map.put(2,"2");
map.put(3,"3");
map.put(4,"2");
Set<Map.Entry<Integer, String>> entries = map.entrySet();
for (Map.Entry<Integer, String> entry : entries) {
System.out.println(entry.getKey());
System.out.println(entry.getValue());
}
}
}
结果:
1
1
2
2
3
3
4
2
public void forEach(BiConsumer<? super K, ? super V> action):遍历
public class Test {
public static void main(String[] args) {
HashMap<Integer,String> map = new HashMap<>(4);
map.put(1,"1");
map.put(2,"2");
map.put(3,"3");
map.put(4,"2");
map.forEach((key,value)->{
System.out.println(key+":"+value);
});
}
}
结果:
1:1
2:2
3:3
4:2
public V getOrDefault(Object key, V defaultValue):key存在返回value,key不存在返回defaultValue
public class Test {
public static void main(String[] args) {
HashMap<Integer,String> map = new HashMap<>(4);
map.put(1,"1");
map.put(2,"2");
map.put(3,"3");
map.put(4,"2");
System.out.println(map.getOrDefault(1,"5"));
System.out.println(map.getOrDefault(5,"6"));
}
}
结果:
1
6
public boolean isEmpty():判断集合是不是空
public class Test {
public static void main(String[] args) {
HashMap<Integer,String> map = new HashMap<>(4);
map.put(1,"1");
map.put(2,"2");
map.put(3,"3");
map.put(4,"2");
System.out.println(map.isEmpty());
}
}
结果:
false
public Set<K> keySet():获取key的set集合
public class Test {
public static void main(String[] args) {
HashMap<Integer,String> map = new HashMap<>(4);
map.put(1,"1");
map.put(2,"2");
map.put(3,"3");
map.put(4,"2");
Set<Integer> integers = map.keySet();
for (Integer integer : integers) {
System.out.println(integer+":"+map.get(integer));
}
}
}
结果:
1:1
2:2
3:3
4:2
public V merge(K key, V value,BiFunction<? super V, ? super V, ? extends V> remappingFunction);key存在修改,不存在新增
public class Test {
public static void main(String[] args) {
HashMap<Integer,String> map = new HashMap<>(4);
map.put(1,"1");
map.put(2,"2");
map.put(3,"3");
map.put(4,"2");
map.merge(1,"1",(key,value)->{
return "test";
});
map.merge(2,"5",(key,value)->{
return "test";
});
map.merge(7,"1",(key,value)->{
return "test";
});
map.merge(5,"6",(key,value)->{
return "test";
});
System.out.println(map);
}
}
结果:
{1=test, 2=test, 3=3, 4=2, 5=6, 7=1}
public void putAll(Map<? extends K, ? extends V> m):putmap
public class Test {
public static void main(String[] args) {
HashMap<Integer,String> map = new HashMap<>(4);
map.put(1,"1");
map.put(2,"2");
map.put(3,"3");
map.put(4,"2");
HashMap<Integer,String> map2 = new HashMap<>(4);
map2.put(5,"7");
map2.put(6,"7");
map.putAll(map2);
System.out.println(map);
}
}
结果:
{1=1, 2=2, 3=3, 4=2, 5=7, 6=7}
public V putIfAbsent(K key, V value):key不存在就添加
public class Test {
public static void main(String[] args) {
HashMap<Integer,String> map = new HashMap<>(4);
map.put(1,"1");
map.put(2,"2");
map.put(3,"3");
map.put(4,"2");
map.putIfAbsent(3,"5");
map.putIfAbsent(4,"2");
map.putIfAbsent(5,"2");
System.out.println(map);
}
}
结果:
{1=1, 2=2, 3=3, 4=2, 5=2}
public boolean remove(Object key, Object value):对应的key、value存在就删除
public class Test {
public static void main(String[] args) {
HashMap<Integer,String> map = new HashMap<>(4);
map.put(1,"1");
map.put(2,"2");
map.put(3,"3");
map.put(4,"2");
map.remove(1,"3");
map.remove(2,"2");
System.out.println(map);
}
}
结果:
{1=1, 3=3, 4=2}
public V replace(K key, V value):替换可以对应的value的值
public class Test {
public static void main(String[] args) {
HashMap<Integer,String> map = new HashMap<>(4);
map.put(1,"1");
map.put(2,"2");
map.put(3,"3");
map.put(4,"2");
map.replace(1,"3");
map.replace(2,"5");
System.out.println(map);
}
}
结果:
{1=3, 2=5, 3=3, 4=2}
public boolean replace(K key, V oldValue, V newValue):替换key、oldValue存在元素的value为newValue
public class Test {
public static void main(String[] args) {
HashMap<Integer,String> map = new HashMap<>(4);
map.put(1,"1");
map.put(2,"2");
map.put(3,"3");
map.put(4,"2");
map.replace(1,"1","3");
map.replace(2,"5","6");
System.out.println(map);
}
}
结果:
{1=3, 2=2, 3=3, 4=2}
public void replaceAll(BiFunction<? super K, ? super V, ? extends V> function):根据自定义规则修改元素
修改key>2的value为test
public class Test {
public static void main(String[] args) {
HashMap<Integer, String> map = new HashMap<>(4);
map.put(1, "1");
map.put(2, "2");
map.put(3, "3");
map.put(4, "2");
map.replaceAll((key, value) -> {
if (key > 2) {
return "test";
}
return value;
});
System.out.println(map);
}
}
结果:
public class Test {
public static void main(String[] args) {
HashMap<Integer, String> map = new HashMap<>(4);
map.put(1, "1");
map.put(2, "2");
map.put(3, "3");
map.put(4, "2");
map.replaceAll((key, value) -> {
if (key > 2) {
return "test";
}
return value;
});
System.out.println(map);
}
}
public int size():map的长度
public Collection<V> values():获取值得集合
public class Test {
public static void main(String[] args) {
HashMap<Integer, String> map = new HashMap<>(4);
map.put(1, "1");
map.put(2, "2");
map.put(3, "3");
map.put(4, "2");
Collection<String> values = map.values();
for (String value : values) {
System.out.println(value);
}
System.out.println(map);
}
}
结果:
1
2
3
2
{1=1, 2=2, 3=3, 4=2}