java map判空工具
时间: 2024-06-14 22:07:20 浏览: 204
以下是Java中判断Map是否为空的工具方法:
1. 使用isEmpty()方法判断Map是否为空:
```java
Map<String, Integer> map = new HashMap<>();
if (map.isEmpty()) {
System.out.println("Map is empty");
} else {
System.out.println("Map is not empty");
}
```
2. 使用size()方法判断Map是否为空:
```java
Map<String, Integer> map = new HashMap<>();
if (map.size() == 0) {
System.out.println("Map is empty");
} else {
System.out.println("Map is not empty");
}
```
3. 使用Optional类判断Map是否为空:
```java
Map<String, Integer> map = new HashMap<>();Optional<Map<String, Integer>> optionalMap = Optional.ofNullable(map);
if (optionalMap.isPresent()) {
System.out.println("Map is not empty");
} else {
System.out.println("Map is empty");
}
```
阅读全文