java - 检查 EmptyOrNull 以获取未知数量的集合和地图
问题描述
我正在尝试util
在 Spring Boot 中实现这样的:
public static boolean isAllEmptyOrNull(Collection... collectionList) {
for (Collection collection : collectionList) {
if (!Collections.isEmpty(collection)) {
return false;
}
}
return true;
}
所以我可以处理以下情况:
- isAllEmptyOrNull(listOfCat);
- isAllEmptyOrNull(listOfDog, mapOfStringToString);
- isAllEmptyOrNull(listOfDog, listOfCat);
- isAllEmptyOrNull(listOfDog, listOfCat, mapOfStringToList, mapOfStringToMap);
任何帮助将不胜感激:)
更新于 2018 年 12 月 6 日
感谢@Deadpool 的帮助,我的解决方案是:
public static boolean isAllCollectionEmptyOrNull(Collection... collections) {
for (Collection collection : collections) {
if (!Collections.isEmpty(collection)) {
return false;
}
}
return true;
}
public static boolean isAllMapEmptyOrNull(Map... maps) {
for (Map map : maps) {
if (!Collections.isEmpty(map)) {
return false;
}
}
return true;
}
当然,你可以像nullpointerstream
一样使用and 。method overloading
解决方案
您可以有两种不同的 util 方法,一种用于检查Collection
对象,另一种用于检查Map
对象,因为Map
它不是Collection
接口的子级
public static boolean isAllEmptyOrNull(Collection... collectionList) {
return Arrays.stream(collectionList).anyMatch(item->item==null || item.isEmpty());
}
public static boolean isAllEmptyOrNull(Map... maps) {
return Arrays.stream(maps).anyMatch(item->item==null || item.isEmpty());
}
检查所有对象null
或empty
public static boolean isAllEmptyOrNull(Collection... collectionList) {
return Arrays.stream(collectionList).allMatch(item->item==null || item.isEmpty());
}
public static boolean isAllEmptyOrNull(Map... maps) {
return Arrays.stream(maps).allMatch(item->item==null || item.isEmpty());
}
推荐阅读
- python - cross_entropy_loss():参数“目标”(位置 2)必须是张量,而不是 numpy.ndarray
- smartcontracts - 智能合约代币分发解决方案
- javascript - drop 事件在 javascript 中不起作用,使用了 preventDefault() 但它不起作用
- reactjs - Redux localStorage 很慢,所以 useEffect 在它之前运行
- reactjs - 材质ui更改选择框的颜色
- angular - 如何清除 ng-select 中的选定项目
- reactjs - 如何在保持不透明度效果的同时将显示转换为显示和隐藏?
- swipe - 如何使用 Siri 遥控器在 Apple TV 上基于向左滑动或向右滑动执行操作?SwiftUI Xcode 13
- c - 如何使用 Do While 来停止循环?
- php - 什么是退货产品的 cs-cart API