java 判断对象是否为空
2023-09-14 08:59:46 时间
* 判断对象或对象数组中每一个对象是否为空: 对象为null,字符序列长度为0,集合类、Map为empty
*
* @param obj
* @return
*/
public static boolean isNullOrEmpty(Object obj) {
if (obj == null)
return true;
if (obj instanceof CharSequence)
return ((CharSequence) obj).length() == 0;
if (obj instanceof Collection)
return ((Collection) obj).isEmpty();
if (obj instanceof Map)
return ((Map) obj).isEmpty();
if (obj instanceof Object[]) {
Object[] object = (Object[]) obj;
if (object.length == 0) {
return true;
}
boolean empty = true;
for (int i = 0; i object.length; i++) {
if (!isNullOrEmpty(object[i])) {
empty = false;
break;
}
}
return empty;
}
return false;
}
public static Object[][] filterEmpty(Object[][] arrays) { int sumNotNull = 0; /*** * 统计非空元素的总个数 */ for (int i = 0; i arrays.length; i++) { Object object = arrays[i]; if (!ValueWidget.isNullOrEmpty(object) !SystemUtil.isNullOrEmpty((Object[]) object)) {// 判断元素是否为空 sumNotNull = sumNotNull + 1; } } Object[][] filtedObjs = new Object[sumNotNull][]; int index = 0; for (int i = 0; i arrays.length; i++) { Object[] object_tmp = arrays[i]; if (!ValueWidget.isNullOrEmpty(object_tmp) !SystemUtil.isNullOrEmpty((Object[]) object_tmp)) {// 判断元素是否为空 filtedObjs[index++] = object_tmp; } } return filtedObjs; }
* @param obj * @param isExcludeZero :true:数值类型的值为0,则当做为空;----false:数值类型的值为0,则不为空 * * @return * @throws SecurityException * @throws IllegalArgumentException * @throws NoSuchFieldException * @throws IllegalAccessException */ public static boolean isNullObject(Object obj, boolean isExcludeZero) throws SecurityException, IllegalArgumentException, NoSuchFieldException, IllegalAccessException { if(ValueWidget.isNullOrEmpty(obj)){//对象本身就为null return true; } List Field fieldList = ReflectHWUtils.getAllFieldList(obj.getClass()); boolean isNull = true; for (int i = 0; i fieldList.size(); i++) { Field f = fieldList.get(i); Object propertyValue = null; try { propertyValue = getObjectValue(obj, f); } catch (NoSuchFieldException e) { e.printStackTrace(); } if (!ValueWidget.isNullOrEmpty(propertyValue)) {// 字段不为空 if (propertyValue instanceof Integer) {// 是数字 if (!((Integer) propertyValue == 0 isExcludeZero)) { isNull = false; break; } } else if (propertyValue instanceof Double) {// 是数字 if (!((Double) propertyValue == 0 isExcludeZero)) { isNull = false; break; } }else if (propertyValue instanceof Float) {// 是数字 if (!((Float) propertyValue == 0 isExcludeZero)) { isNull = false; break; } }else if (propertyValue instanceof Short) {// 是数字 if (!((Short) propertyValue == 0 isExcludeZero)) { isNull = false; break; } }else { isNull = false; break; } } } return isNull; }
public void test_isNullObject() throws SecurityException, IllegalArgumentException, NoSuchFieldException, IllegalAccessException { Person2 p = new Person2(); Assert.assertEquals(true, ReflectHWUtils.isNullObject(p, true)); Assert.assertEquals(false, ReflectHWUtils.isNullObject(p, false)); p.setAddress("beijing"); Assert.assertEquals(false, ReflectHWUtils.isNullObject(p, true)); Assert.assertEquals(false, ReflectHWUtils.isNullObject(p, false)); p.setAddress(null); p.setId(0); Assert.assertEquals(true, ReflectHWUtils.isNullObject(p, true)); Assert.assertEquals(false, ReflectHWUtils.isNullObject(p, false)); }
Java的对象克隆 Object 类提供的 clone() 方法是 “浅拷贝”,并没有克隆对象中引用的其他对象,原对象和克隆的对象仍然会共享一些信息。深拷贝指的是:在对象中存在其他对象的引用的情况下,会同时克隆对象中引用的其他对象,原对象和克隆的对象互不影响。
Java对象的序列化和反序列化 Java 对象的序列化和反序列化是一种将对象转换成字节流并存储在硬盘或网络中,以及从字节流中重新加载对象的操作。Java 的序列化和反序列化提供了一种方便的方式,使得可以将对象在不同的应用程序之间进行交互。
黄威的世界 我是一个热衷IT技术的人,希望自己不断地设计开发出对别人非常有用的软件。有近7年的java开发经验(包括2年Android开发经验)和一年左右的linux使用经验。擅长Java Web后台开发 ,喜欢研究新的各种实用技术
读取excel文件,转化为一个二维数组:Object[][] arrays
但是excel中有空行,所以需要过滤Object[][] arrays中的空的一维数组:
public static Object[][] filterEmpty(Object[][] arrays) { int sumNotNull = 0; /*** * 统计非空元素的总个数 */ for (int i = 0; i arrays.length; i++) { Object object = arrays[i]; if (!ValueWidget.isNullOrEmpty(object) !SystemUtil.isNullOrEmpty((Object[]) object)) {// 判断元素是否为空 sumNotNull = sumNotNull + 1; } } Object[][] filtedObjs = new Object[sumNotNull][]; int index = 0; for (int i = 0; i arrays.length; i++) { Object[] object_tmp = arrays[i]; if (!ValueWidget.isNullOrEmpty(object_tmp) !SystemUtil.isNullOrEmpty((Object[]) object_tmp)) {// 判断元素是否为空 filtedObjs[index++] = object_tmp; } } return filtedObjs; }
* @param obj * @param isExcludeZero :true:数值类型的值为0,则当做为空;----false:数值类型的值为0,则不为空 * * @return * @throws SecurityException * @throws IllegalArgumentException * @throws NoSuchFieldException * @throws IllegalAccessException */ public static boolean isNullObject(Object obj, boolean isExcludeZero) throws SecurityException, IllegalArgumentException, NoSuchFieldException, IllegalAccessException { if(ValueWidget.isNullOrEmpty(obj)){//对象本身就为null return true; } List Field fieldList = ReflectHWUtils.getAllFieldList(obj.getClass()); boolean isNull = true; for (int i = 0; i fieldList.size(); i++) { Field f = fieldList.get(i); Object propertyValue = null; try { propertyValue = getObjectValue(obj, f); } catch (NoSuchFieldException e) { e.printStackTrace(); } if (!ValueWidget.isNullOrEmpty(propertyValue)) {// 字段不为空 if (propertyValue instanceof Integer) {// 是数字 if (!((Integer) propertyValue == 0 isExcludeZero)) { isNull = false; break; } } else if (propertyValue instanceof Double) {// 是数字 if (!((Double) propertyValue == 0 isExcludeZero)) { isNull = false; break; } }else if (propertyValue instanceof Float) {// 是数字 if (!((Float) propertyValue == 0 isExcludeZero)) { isNull = false; break; } }else if (propertyValue instanceof Short) {// 是数字 if (!((Short) propertyValue == 0 isExcludeZero)) { isNull = false; break; } }else { isNull = false; break; } } } return isNull; }
public void test_isNullObject() throws SecurityException, IllegalArgumentException, NoSuchFieldException, IllegalAccessException { Person2 p = new Person2(); Assert.assertEquals(true, ReflectHWUtils.isNullObject(p, true)); Assert.assertEquals(false, ReflectHWUtils.isNullObject(p, false)); p.setAddress("beijing"); Assert.assertEquals(false, ReflectHWUtils.isNullObject(p, true)); Assert.assertEquals(false, ReflectHWUtils.isNullObject(p, false)); p.setAddress(null); p.setId(0); Assert.assertEquals(true, ReflectHWUtils.isNullObject(p, true)); Assert.assertEquals(false, ReflectHWUtils.isNullObject(p, false)); }
Java的对象克隆 Object 类提供的 clone() 方法是 “浅拷贝”,并没有克隆对象中引用的其他对象,原对象和克隆的对象仍然会共享一些信息。深拷贝指的是:在对象中存在其他对象的引用的情况下,会同时克隆对象中引用的其他对象,原对象和克隆的对象互不影响。
Java对象的序列化和反序列化 Java 对象的序列化和反序列化是一种将对象转换成字节流并存储在硬盘或网络中,以及从字节流中重新加载对象的操作。Java 的序列化和反序列化提供了一种方便的方式,使得可以将对象在不同的应用程序之间进行交互。
黄威的世界 我是一个热衷IT技术的人,希望自己不断地设计开发出对别人非常有用的软件。有近7年的java开发经验(包括2年Android开发经验)和一年左右的linux使用经验。擅长Java Web后台开发 ,喜欢研究新的各种实用技术
相关文章
- JAVA-JSP内置对象之application对象
- Java实现 LeetCode 713 乘积小于K的子数组(子集数量+双指针)
- Java实现 LeetCode 417 太平洋大西洋水流问题
- Java实现打印回型嵌套
- Java实现 蓝桥杯VIP 算法训练 数对
- 【JAVA】 02-Java对象细节
- Eclipse 报 “Exception in thread "main" java.lang.OutOfMemoryError: Java heap space ”错误的解决办法
- 【JAVA】java编译错误:编码UTF8/GBK的不可映射字符
- 【Java】java使用反射访问对象方法和成员变量
- java 使用tess4j实现OCR的最简单样例
- Java Spring MVC model学习
- Java项目练习:软件项目信息管理(附源码下载地址)
- 分析substring(),charAt()等常见java.lang.StringIndexOutOfBoundsException: String index out of range: -1的问题
- 从jvm的角度考虑链表是如和存储的,并手写Java单向链表的,问题难在节点和头节点的对象引用
- 【性能优化】面试官:Java中的对象都是在堆上分配的吗?
- Java Instrumentation 内存马——主要是利用Instrumentation Java API来做内存注入,会用到反射机制,文中提到检测思路:注入jar包-> dump已加载class字节码->反编译成java代码-> 源码webshell检测
- Java观察者模式:轻松实现对象间的一对多依赖
- 【java基础】条件判断,if、switch
- 【java】Java 接口(Interface)
- 基于JAVA实现的WEB端UI自动化 - WebDriver高级篇 - 获取测试对象属性