# 常用方法
# 只会用 map.put?试试 Java 8 compute
- compute——计算并更新值
- computeIfAbsent——Value不存在时才计算
- computeIfPresent——Value存在时才计算
List<String> animals = Lists.newArrayList("dog", "cat", "mouse", "cat", "rabbit", "cat");
Map<String, Integer> map = new HashMap<>(8);
map.put("dog", 23);
animals.forEach(item -> map.computeIfPresent(item, (key, value) -> ++value));
System.out.println(map);
// {dog=24}
List<String> animals = Lists.newArrayList("dog", "cat", "mouse", "cat", "rabbit", "cat");
Map<String, Integer> map = new HashMap<>(8);
map.put("dog", 23);
animals.forEach(item -> map.compute(item, (key, value) -> value == null ? 10 : (value + 1) * 10));
System.out.println(map);
// {mouse=10, dog=240, rabbit=10, cat=1110}
List<String> animals = Lists.newArrayList("dog", "cat", "mouse", "cat", "rabbit", "cat");
Map<String, Integer> map = new HashMap<>(8);
map.put("dog", 23);
animals.forEach(item -> map.computeIfAbsent(item, key -> 33));
System.out.println(map);
// {mouse=33, dog=23, rabbit=33, cat=33}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# Hashmap的存值
import java.util.HashMap;
public class TestDemo{
public static void main(String[] args) {
HashMap<String, Integer> map=new HashMap<>();
// 向map中添加值(返回这个key以前的值,如果没有返回null)
System.out.println(map.put("test", 1));
// 向map中添加值(返回这个key以前的值,如果没有返回null)
System.out.println(map.put("test", 2));
}
}
1
2
3
4
5
6
7
8
9
10
2
3
4
5
6
7
8
9
10
执行结果如下:
null
1
1
2
2
# Hashmap的取值
import java.util.HashMap;
public class TestDemo{
public static void main(String[] args) {
HashMap<String, Integer> map=new HashMap<>();
map.put("test", 1);
// 取map中key相对应的value的值
System.out.println(map.get("ops"));
System.out.println(map.get("test"));
}
}
1
2
3
4
5
6
7
8
9
10
2
3
4
5
6
7
8
9
10
执行结果如下:
null
1
1
2
2
# Hashmap 判断是否为空
import java.util.HashMap;
public class TestDemo{
public static void main(String[] args) {
HashMap<String, Integer> map=new HashMap<>();
// 判断map是否为空
System.out.println(map.isEmpty());
map.put("test", 1);
System.out.println(map.isEmpty());
}
}
1
2
3
4
5
6
7
8
9
10
2
3
4
5
6
7
8
9
10
执行结果如下:
true
false
1
2
2
# Hashmap 判断是否含有key
import java.util.HashMap;
public class TestDemo{
public static void main(String[] args) {
HashMap<String, Integer> map=new HashMap<>();
//判断map中是否存在这个key
System.out.println(map.containsKey("ops"));
map.put("test", 1);
System.out.println(map.containsKey("test"));
}
}
1
2
3
4
5
6
7
8
9
10
2
3
4
5
6
7
8
9
10
执行结果如下:
false
true
1
2
2
# Hashmap 判断是否含有value
import java.util.HashMap;
public class TestDemo{
public static void main(String[] args) {
HashMap<String, Integer> map=new HashMap<>();
// 判断map中是否存在这个value
System.out.println(map.containsValue(1));
map.put("test", 1);
System.out.println(map.containsValue(1));
}
}
1
2
3
4
5
6
7
8
9
10
2
3
4
5
6
7
8
9
10
执行结果如下:
false
true
1
2
2
# Hashmap 删除key值下的value
import java.util.HashMap;
public class TestDemo{
public static void main(String[] args) {
HashMap<String, Integer> map=new HashMap<>();
// 删除key值下的value
System.out.println(map.remove("ops"));
map.put("test", 2);
System.out.println(map.remove("test"));
System.out.println(map.get("test"));
}
}
1
2
3
4
5
6
7
8
9
10
11
2
3
4
5
6
7
8
9
10
11
执行结果如下:
null
2
null
1
2
3
2
3
# Hashmap 获取所有的value值
import java.util.HashMap;
public class TestDemo{
public static void main(String[] args) {
HashMap<String, Integer> map=new HashMap<>();
//显示所有的value值
System.out.println(map.values());
map.put("test", 1);
map.put("ops", 2);
System.out.println(map.values());
}
}
1
2
3
4
5
6
7
8
9
10
11
2
3
4
5
6
7
8
9
10
11
执行结果如下:
[]
[2, 1]
1
2
2
# Hashmap 获取所有的key值
import java.util.HashMap;
public class TestDemo{
public static void main(String[] args) {
HashMap<String, Integer> map=new HashMap<>();
//显示map所有的key
System.out.println(map.keySet());
map.put("test", 1);
System.out.println(map.keySet());
map.put("ops", 2);
System.out.println(map.keySet());
}
}
1
2
3
4
5
6
7
8
9
10
11
12
2
3
4
5
6
7
8
9
10
11
12
执行结果如下:
[]
[test]
[ops, test]
1
2
3
2
3
# Hashmap 获取元素个数
Hashmap 获取所有的value值
import java.util.HashMap;
public class TestDemo{
public static void main(String[] args) {
HashMap<String, Integer> map=new HashMap<>();
//显示map里的值得数量
System.out.println(map.size());
map.put("test", 1);
System.out.println(map.size());
map.put("ops", 2);
System.out.println(map.size());
}
}
1
2
3
4
5
6
7
8
9
10
11
12
2
3
4
5
6
7
8
9
10
11
12
执行结果如下:
0
1
2
1
2
3
2
3
# Hashmap 获取所有的key和value
import java.util.HashMap;
public class TestDemo {
public static void main(String[] args) {
HashMap<String, Integer> map = new HashMap<>();
// 显示所有的key和value
System.out.println(map.entrySet());
map.put("test", 1);
System.out.println(map.entrySet());
map.put("ops", 2);
System.out.println(map.entrySet());
}
}
1
2
3
4
5
6
7
8
9
10
11
12
2
3
4
5
6
7
8
9
10
11
12
执行结果如下:
[]
[test=1]
[ops=2, test=1]
1
2
3
2
3
# Hashmap 添加另一个同一类型的map下的所有的KV值
import java.util.HashMap;
public class TestDemo {
public static void main(String[] args) {
HashMap<String, Integer> mapO=new HashMap<>();
HashMap<String, Integer> mapT=new HashMap<>();
// 将同一类型的map添加到另一个map中
mapT.put("test", 1);
mapO.put("ops", 2);
mapO.put("test", 2);
System.out.println(mapO);
mapO.putAll(mapT);
System.out.println(mapO);
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
2
3
4
5
6
7
8
9
10
11
12
13
14
执行结果如下:
{ops=2, test=2}
{ops=2, test=1}
1
2
2
# Hashmap 删除对应key和value
import java.util.HashMap;
public class TestDemo {
public static void main(String[] args) {
HashMap<String, Integer> map=new HashMap<>();
// 删除这个键值对
map.put("test", 1);
map.put("ops", 2);
System.out.println(map);
System.out.println(map.remove("ops", 1));
System.out.println(map.remove("ops", 2));
System.out.println(map);
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
2
3
4
5
6
7
8
9
10
11
12
13
执行结果如下:
{ops=2, test=1}
false
true
{test=1}
1
2
3
4
2
3
4
# Hashmap 替换 key的value
import java.util.HashMap;
public class TestDemo {
public static void main(String[] args) {
HashMap<String, Integer> map=new HashMap<>();
//判断map中是否存在这个key
map.put("test", 1);
map.put("ops", 2);
System.out.println(map);
System.out.println(map.replace("ops", 1));
System.out.println(map);
}
}
1
2
3
4
5
6
7
8
9
10
11
12
2
3
4
5
6
7
8
9
10
11
12
执行结果如下:
{ops=2, test=1}
2
{ops=1, test=1}
1
2
3
2
3
# Hashmap 清空
import java.util.HashMap;
public class TestDemo {
public static void main(String[] args) {
HashMap<String, Integer> map=new HashMap<>();
// 清空map
map.put("test", 1);
map.put("ops", 2);
System.out.println(map);
map.clear();
System.out.println(map);
}
}
1
2
3
4
5
6
7
8
9
10
11
12
2
3
4
5
6
7
8
9
10
11
12
执行结果如下:
{ops=2, test=1}
{}
1
2
2
# Hashmap 克隆
import java.util.HashMap;
public class TestDemo {
public static void main(String[] args) {
HashMap<String, Integer> map=new HashMap<>();
// 克隆map
map.put("test", 1);
map.put("ops", 2);
System.out.println(map.clone());
Object cloneMap = map.clone();
System.out.println(cloneMap);
}
}
1
2
3
4
5
6
7
8
9
10
11
12
2
3
4
5
6
7
8
9
10
11
12
执行结果如下:
{test=1, ops=2}
{test=1, ops=2}
1
2
2