如何使用Collection判断HashMap不为空
时间: 2024-01-22 20:03:28 浏览: 30
可以使用Collection的isEmpty()方法判断HashMap的values集合是否为空,如果values集合不为空,则说明HashMap不为空。示例代码如下:
```java
HashMap<String, Integer> hashMap = new HashMap<>();
// 添加键值对
hashMap.put("A", 1);
hashMap.put("B", 2);
if (!hashMap.values().isEmpty()) {
System.out.println("HashMap不为空");
} else {
System.out.println("HashMap为空");
}
```
如果HashMap不为空,输出结果为"HashMap不为空",否则输出结果为"HashMap为空"。
相关问题
用java写一个判断多种类型的参数是否为空的工具类
可以写一个 `ParamUtil` 工具类来判断多种类型的参数是否为空,具体实现如下:
```java
public class ParamUtil {
/**
* 判断字符串是否为空
*
* @param str 待判断的字符串
* @return 是否为空
*/
public static boolean isEmpty(String str) {
return str == null || str.trim().length() == 0;
}
/**
* 判断集合是否为空
*
* @param collection 待判断的集合
* @return 是否为空
*/
public static boolean isEmpty(Collection<?> collection) {
return collection == null || collection.isEmpty();
}
/**
* 判断数组是否为空
*
* @param array 待判断的数组
* @return 是否为空
*/
public static boolean isEmpty(Object[] array) {
return array == null || array.length == 0;
}
/**
* 判断Map是否为空
*
* @param map 待判断的Map
* @return 是否为空
*/
public static boolean isEmpty(Map<?, ?> map) {
return map == null || map.isEmpty();
}
/**
* 判断对象是否为空
*
* @param obj 待判断的对象
* @return 是否为空
*/
public static boolean isEmpty(Object obj) {
if (obj == null) {
return true;
}
if (obj instanceof String) {
return isEmpty((String) obj);
}
if (obj instanceof Collection) {
return isEmpty((Collection<?>) obj);
}
if (obj instanceof Object[]) {
return isEmpty((Object[]) obj);
}
if (obj instanceof Map) {
return isEmpty((Map<?, ?>) obj);
}
return false;
}
}
```
使用时可以直接调用 `ParamUtil.isEmpty()` 方法,例如:
```java
String str = null;
if (ParamUtil.isEmpty(str)) {
System.out.println("字符串为空");
}
List<String> list = new ArrayList<>();
if (ParamUtil.isEmpty(list)) {
System.out.println("集合为空");
}
String[] array = new String[0];
if (ParamUtil.isEmpty(array)) {
System.out.println("数组为空");
}
Map<String, Object> map = new HashMap<>();
if (ParamUtil.isEmpty(map)) {
System.out.println("Map为空");
}
Object obj = null;
if (ParamUtil.isEmpty(obj)) {
System.out.println("对象为空");
}
```
实现 hashmap对象创建
要实现HashMap对象的创建,可以按照以下步骤进行操作:
1. 首先,在代码中导入HashMap类的包,例如:import java.util.HashMap;
2. 然后,创建一个HashMap对象,例如:HashMap<String, String> map = new HashMap<String, String>();
3. 在创建HashMap对象时,可以指定键和值的类型,例如上述代码中的String类型。
4. 接下来,可以使用put()方法向HashMap中添加键值对,例如:map.put("key1", "value1");
5. 可以使用get()方法获取HashMap中指定键对应的值,例如:String value = map.get("key1");
6. 可以使用remove()方法删除HashMap中指定键对应的键值对,例如:map.remove("key1");
7. 可以使用size()方法获取HashMap中键值对的数量,例如:int size = map.size();
8. 可以使用isEmpty()方法判断HashMap是否为空,例如:boolean empty = map.isEmpty();
9. 可以使用keySet()方法获取HashMap中所有键的集合,例如:Set<String> keySet = map.keySet();
10. 可以使用values()方法获取HashMap中所有值的集合,例如:Collection<String> values = map.values()。
范例:<<引用:插图1:使用put()方法在HashMap中创建和添加对象 插图2:使用size()方法获取HashMap的大小 插图3:使用get()方法在HashMap中检索对象 插图4:使用remove()方法从HashMap中删除对象 插图5: containsKey...。>>
例如,以下代码演示了如何创建一个HashMap对象,并向其中添加键值对:
```
import java.util.HashMap;
public class Example {
public static void main(String[] args) {
HashMap<String, Integer> map = new HashMap<String, Integer>();
map.put("apple", 1);
map.put("banana", 2);
map.put("orange", 3);
System.out.println(map); // 输出:{orange=3, banana=2, apple=1}
}
}
```
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)