list 过滤掉对象属性为null对象
时间: 2024-04-15 16:28:00 浏览: 26
你可以使用列表推导式 (list comprehension) 来过滤掉属性为 null 的对象。以下是一个示例代码:
```python
data = [obj1, obj2, obj3] # 假设这是你的对象列表
filtered_data = [obj for obj in data if obj is not None]
print(filtered_data)
```
在这个示例中,我们使用了列表推导式来创建一个新的列表 `filtered_data`,其中只包含属性不为 null 的对象。`if obj is not None` 是过滤条件,只有当对象不为 null 时才会被包含在结果列表中。你可以根据自己的需求调整过滤条件。
相关问题
stream流校验对象属性是否为空
可以使用 Stream 流的 filter 和 anyMatch 方法来校验对象属性是否为空。
假设有一个包含多个对象的 List,每个对象都有一个属性 name,我们需要校验这个属性是否为空。可以按照以下步骤操作:
1. 使用 Stream 流将 List 转换为一个 Stream 对象。
2. 使用 filter 方法过滤出 name 属性为空的对象。
3. 使用 anyMatch 方法判断是否存在 name 属性为空的对象。
示例代码如下:
```
List<User> userList = getUsers();
boolean isNameEmpty = userList.stream()
.filter(user -> user.getName() == null || user.getName().isEmpty())
.anyMatch(user -> true);
```
在上面的代码中,getUsers() 方法返回一个包含多个 User 对象的 List。我们首先使用 stream() 方法将其转换为一个 Stream 对象,然后使用 filter 方法过滤出 name 属性为空的对象,最后使用 anyMatch 方法判断是否存在 name 属性为空的对象。如果存在,则 isNameEmpty 的值为 true,否则为 false。
需要注意的是,如果对象属性是基本数据类型,那么不能使用 == null 判断是否为空,应该使用相应的判断方法,比如 int 类型使用 == 0。如果对象属性是 Boolean 类型,那么不能使用 isEmpty 方法判断是否为空,应该使用 == null 判断。
BeanUtils过滤掉空属性
BeanUtils不提供过滤空属性的方法,但可以通过编写自定义的BeanUtils.copyProperties方法来实现。
以下是一个示例:
```
public static void copyPropertiesIgnoreNull(Object source, Object target) {
BeanUtilsBean beanUtils = new BeanUtilsBean(new ConvertUtils() {
@Override
public Object convert(String value, Class clazz) {
if (clazz.isEnum()) {
return Enum.valueOf(clazz, value.toString());
} else {
return super.convert(value, clazz);
}
}
});
PropertyUtilsBean propertyUtils = beanUtils.getPropertyUtils();
PropertyDescriptor[] descriptors = propertyUtils.getPropertyDescriptors(target);
List<String> ignoreProperties = new ArrayList<>();
for (PropertyDescriptor propertyDescriptor : descriptors) {
String name = propertyDescriptor.getName();
Object value;
try {
value = propertyUtils.getSimpleProperty(target, name);
} catch (Exception e) {
throw new RuntimeException(e);
}
if (value == null) {
ignoreProperties.add(name);
}
}
ignoreProperties.add("class");
String[] ignoreArray = ignoreProperties.toArray(new String[ignoreProperties.size()]);
try {
BeanUtils.copyProperties(target, source, ignoreArray);
} catch (IllegalAccessException | InvocationTargetException e) {
throw new RuntimeException(e);
}
}
```
该方法会将目标对象中值为null的属性过滤掉,从而避免将null值赋给目标对象的属性。
相关推荐
![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)
![](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)