当前位置:网站首页>使用工具类把对象中的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;
}
边栏推荐
- openpyxl to manipulate Excel files
- Analysis and practice of antjian webshell dynamic encrypted connection
- 无题十
- 交换机端口的三种类型详解与hybrid端口实验
- 请问如果想往mysql里面写数据,直接用flink-connector-jdbc就可以吧,可是我在f
- Example of Noise Calculation for Amplifier OPA855
- ffmpeg drawtext add text watermark
- 正则表达式replaceAll()方法具有什么功能呢?
- 如何实现按键的短按、长按检测?
- 并发之CAS
猜你喜欢
上海控安技术成果入选市经信委《2021年上海市网络安全产业创新攻关成果目录》
链表中的数字相加----链表专题
The technological achievements of Shanghai Konan were selected into the "2021 Shanghai Network Security Industry Innovation Research Achievement Catalog" by the Municipal Commission of Economy and Inf
express hot-reload
Creo 9.0 基准特征:基准轴
Dry goods!Generative Model Evaluation and Diagnosis
【zeno】为zeno增加子模块/新节点的最小化的例子
基于 Kubernetes 的微服务项目整体设计与实现
轩辕实验室丨欧盟EVITA项目预研 第一章(四)
Egg framework usage (2)
随机推荐
明天去订票,准备回家咯~~
Happens-before rules for threads
The technological achievements of Shanghai Konan were selected into the "2021 Shanghai Network Security Industry Innovation Research Achievement Catalog" by the Municipal Commission of Economy and Inf
Assembly language (8) x86 inline assembly
科普大佬说 | 港大黄凯斌老师带你解锁黑客帝国与6G的关系
Imitation SBUS fixed with serial data conversion
微服务 技术栈
leetcode points to Offer 10- I. Fibonacci sequence
5. Deploy the web project to the cloud server
What is the function of the regular expression replaceAll() method?
如何实现按键的短按、长按检测?
Creo 9.0 基准特征:基准轴
Concurrent CAS
无题十二
CPU的亲缘性affinity
轩辕实验室丨欧盟EVITA项目预研 第一章(四)
汇编语言(8)x86内联汇编
What is the function of the regular expression replaceFirst() method?
Hundred lines of code launch red hearts, why programmers lose their girlfriends!
dotnet OpenXML parsing PPT charts Getting started with area charts