当前位置:网站首页>使用工具类把对象中的null值转换为空字符串(集合也可以使用)
使用工具类把对象中的null值转换为空字符串(集合也可以使用)
2022-08-05 09:35:00 【BOBO阿】
把单个对象中的String类型的null字段,转换为空字符串
/** * 把单个对象中的String类型的null字段,转换为空字符串 * 注意:只能转换String类型的字段 * @param <T> 待转化对象类型 * @param cls 待转化对象 * @return 转化好的对象 */
public static <T> T nullToString(T cls) {
Field[] fields = cls.getClass().getDeclaredFields();
if (fields == null || fields.length == 0) {
return cls;
}
for (Field field : fields) {
if ("String".equals(field.getType().getSimpleName())) {
field.setAccessible(true);
try {
Object value = field.get(cls);
if (value == null) {
field.set(cls, "");
}
} catch (IllegalArgumentException | IllegalAccessException e) {
e.printStackTrace();
}
}
}
return cls;
}
把集合中的所有对象中的String类型的null字段,转换为空字符串
/** * 把集合中的所有对象中的String类型的null字段,转换为空字符串 * 注意:只能转换String类型的字段 * @param sourceList 待转化的集合 * @return 转化好的集合 */
public static <T> List<T> listNullToString(List<T> sourceList) {
ArrayList<T> resultList = new ArrayList<>();
for (T cls : sourceList) {
Field[] fields = cls.getClass().getDeclaredFields();
if (fields == null || fields.length == 0) {
resultList.add(cls);
}
for (Field field : fields) {
if ("String".equals(field.getType().getSimpleName())) {
field.setAccessible(true);
try {
Object value = field.get(cls);
if (value == null) {
field.set(cls, "");
}
} catch (IllegalArgumentException | IllegalAccessException e) {
e.printStackTrace();
}
}
}
resultList.add(cls);
}
return resultList;
}
边栏推荐
- There is only one switch, how to realize the nqa of master-slave automatic switching
- 无题六
- MySQL advanced (twenty-seven) database index principle
- Going to book tickets tomorrow, ready to go home~~
- Excuse me, guys, is it impossible to synchronize two databases in real time using Flink SQL CDC?
- 为什么我推荐使用智能化async?
- Creo 9.0 基准特征:基准点
- 2022.8.3
- openpyxl to manipulate Excel files
- Microservice Technology Stack
猜你喜欢

CCVR基于分类器校准缓解异构联邦学习

如何实现按键的短按、长按检测?

【ASM】字节码操作 方法的初始化 Frame

Creo 9.0 基准特征:基准坐标系

Science bosses say | Hong Kong rhubarb KaiBin teacher take you unlock the relationship between the matrix and 6 g

pytorch余弦退火学习率CosineAnnealingLR的使用

Example of Noise Calculation for Amplifier OPA855

Keil升级到AC6后,到底有哪些变化?

Custom filters and interceptors implement ThreadLocal thread closure

营销建议 | 您有一份八月营销月历待查收! 建议收藏 !
随机推荐
长达四年的减肥记录
pytorch余弦退火学习率CosineAnnealingLR的使用
Weekly Report 2022-8-4
Pytorch Deep Learning Quick Start Tutorial -- Mound Tutorial Notes (3)
5. Deploy the web project to the cloud server
交换机端口的三种类型详解与hybrid端口实验
Assembly language (8) x86 inline assembly
C语言的高级用法
无题一
Excuse me if you want to write data in mysql, with flink - connector - JDBC directly is ok, but I'm in the f
Egg framework usage (2)
19.服务器端会话技术Session
Analysis and practice of antjian webshell dynamic encrypted connection
【Excel实战】--图表联动demo_001
egg框架使用(二)
PAT Grade B-B1020 Mooncake(25)
2022-08-01 回顾基础二叉树以及操作
After Keil upgrades to AC6, what changes?
如何实现按键的短按、长按检测?
【zeno】为zeno增加子模块/新节点的最小化的例子