CollectionUtils.isEmpty()和map.isEmpty()的区别
时间: 2024-05-18 18:15:05 浏览: 8
`CollectionUtils.isEmpty()`是Apache Commons Lang库中的一个方法,用于判断一个集合是否为空,如果集合为null或者长度为0,则返回true,否则返回false。而`Map.isEmpty()`是Java自带的Map接口中的一个方法,用于判断一个Map是否为空,如果Map为null或者Map中没有任何键值对,则返回true,否则返回false。
因此,`CollectionUtils.isEmpty()`可以用于判断List、Set等集合类型是否为空,而`Map.isEmpty()`则只能用于判断Map是否为空。同时,对于Map类型,也可以使用`CollectionUtils.isEmpty(map)`来判断Map是否为空,但是`Map.isEmpty()`更为直观和简洁。
相关问题
代码优化 res.add(CollectionUtils.isEmpty(typeMap.get(type)) ? 0 : typeMap.get(type).size());
这段代码的意思是:如果typeMap中类型为type的元素为空,则向res中加入0,否则将typeMap中类型为type的元素的大小加入到res中。这段代码可以进行一些优化。首先,可以将CollectionUtils.isEmpty(typeMap.get(type)) ? 0 : typeMap.get(type).size()提取为一个局部变量,以提高代码的可读性和可维护性。其次,可以使用Java 8的Optional类来简化代码,如下所示:
int size = Optional.ofNullable(typeMap.get(type)).map(Collection::size).orElse(0);
res.add(size);
这段代码与上面的代码实现的功能是相同的,但使用了Java 8的Lambda表达式和Optional类进行改写,可以让代码更加简洁和优雅。
private List<ChannelTypeTenantVO> filterUseType(List<ChannelTypeTenantVO> channelTypes, List<String> filterList){ if(CollectionUtils.isEmpty(channelTypes) || CollectionUtils.isEmpty(filterList)) { return channelTypes; } return channelTypes.stream().filter(v -> { if(StringUtils.isEmpty(v.getUseCondition())) { return true; } ChannelTypeVO.UseConditionVO useConditionVO = JsonUtils.readValueQuietly(v.getUseCondition(), ChannelTypeVO.UseConditionVO.class); if(BooleanUtils.isNotTrue(useConditionVO.getUseTypeFlag())) { return true; } List<String> useTypeList = useConditionVO.getUseTypeList(); if(CollectionUtils.isNotEmpty(useTypeList) && !Collections.disjoint(useTypeList, filterList)) { return true; } return false; }).collect(Collectors.toList()); } private List<ChannelTypeVO> filterUseType1(List<ChannelTypeVO> channelTypes, List<String> filterList){ if(CollectionUtils.isEmpty(channelTypes) || CollectionUtils.isEmpty(filterList)) { return channelTypes; } return channelTypes.stream().filter(v -> { if(StringUtils.isEmpty(v.getUseCondition())) { return true; } ChannelTypeVO.UseConditionVO useConditionVO = JsonUtils.readValueQuietly(v.getUseCondition(), ChannelTypeVO.UseConditionVO.class); if(BooleanUtils.isNotTrue(useConditionVO.getUseTypeFlag())) { return true; } List<String> useTypeList = useConditionVO.getUseTypeList(); if(CollectionUtils.isNotEmpty(useTypeList) && !Collections.disjoint(useTypeList, filterList)) { return true; } return false; }).collect(Collectors.toList()); }
这段代码是两个方法,它们的方法名和实现相同,只是所处理的数据类型不同。第一个方法是 `filterUseType`,它接受一个 `List<ChannelTypeTenantVO>` 类型的参数 `channelTypes` 和一个 `List<String>` 类型的参数 `filterList`,返回一个经过过滤后的 `List<ChannelTypeTenantVO>` 类型的列表。第二个方法是 `filterUseType1`,它接受一个 `List<ChannelTypeVO>` 类型的参数 `channelTypes` 和一个 `List<String>` 类型的参数 `filterList`,返回一个经过过滤后的 `List<ChannelTypeVO>` 类型的列表。这样的重复代码是不必要的,可以将它们合并为一个方法,使用泛型来处理不同类型的数据。
可以将这两个方法合并为一个泛型方法,它接受一个 `List<T>` 类型的参数 `channelTypes`,一个 `List<String>` 类型的参数 `filterList`,以及一个 `Class<T>` 类型的参数 `clazz`,用于指定 `channelTypes` 中元素的类型。其中的逻辑实现和原来的方法是相同的。例如:
```
private <T> List<T> filterUseType(List<T> channelTypes, List<String> filterList, Class<T> clazz) {
if(CollectionUtils.isEmpty(channelTypes) || CollectionUtils.isEmpty(filterList)) {
return channelTypes;
}
return channelTypes.stream().filter(v -> {
if(StringUtils.isEmpty(v.getUseCondition())) {
return true;
}
ChannelTypeVO.UseConditionVO useConditionVO = JsonUtils.readValueQuietly(v.getUseCondition(), ChannelTypeVO.UseConditionVO.class);
if(BooleanUtils.isNotTrue(useConditionVO.getUseTypeFlag())) {
return true;
}
List<String> useTypeList = useConditionVO.getUseTypeList();
if(CollectionUtils.isNotEmpty(useTypeList) && !Collections.disjoint(useTypeList, filterList)) {
return true;
}
return false;
}).map(clazz::cast).collect(Collectors.toList());
}
```
这样,你就可以在调用方法时传入不同类型的数据,而不需要重复编写相似的代码。例如,你可以这样调用该方法:
```
List<ChannelTypeTenantVO> channelTypes = ...;
List<String> filterList = ...;
List<ChannelTypeTenantVO> result1 = filterUseType(channelTypes, filterList, ChannelTypeTenantVO.class);
List<ChannelTypeVO> channelTypes2 = ...;
List<String> filterList2 = ...;
List<ChannelTypeVO> result2 = filterUseType(channelTypes2, filterList2, ChannelTypeVO.class);
```