javaempty的简单介绍

博主:adminadmin 2023-01-13 02:06:09 377

本篇文章给大家谈谈javaempty,以及对应的知识点,希望对各位有所帮助,不要忘了收藏本站喔。

本文目录一览:

java对象为空的判断

/** 

     * 判断对象或对象数组中每一个对象是否为空: 对象为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;  

    }  

应用场景:

读取excel文件,转化为一个二维数组:Object[][] arrays

但是excel中有空行,所以需要过滤Object[][] arrays中的空的一维数组:

Java代码  

/*** 

     * 过滤数组中的空元素 

     *  

     *  

     * @param arrays 

     * @return 

     */  

    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;  

    }  

判断对象的所有成员变量是否为空

Java代码  

/*** 

     * Determine whether the object's fields are empty 

     *  

     * @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;  

        }  

        ListField 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;  

    }  

 测试:

Java代码  

@Test  

    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));  

  

    }  

 Person2 源代码(省略getter,setter方法):

Java代码  

import java.sql.Timestamp;  

  

public class Person2 {  

    private int id;  

    private int age;  

    private double weight;  

    private String personName;  

    private Timestamp birthdate;  

    public String identitify;  

    protected String address;  

    String phone;  

      

}

在做java开发的时候,让后台参数传到前台,后台设定的为“dataList”,那么在下面的程序块中“empty ”是

empty 其实是一个关键字。相当于null 。

c:if test="${!empty dataList}" 这句话的意思是,如果dataList不为空的话,走c:forEach items="${dataList}" var="data" 这句代码。

JAVA中isEmpty和null以及""的区别

这两者的区别就在于有没有分配内存。

null是没有分配内存,isEmpty是分配了内存,但没有放数据

Java中isEmpty方法如何使用?

isEmpty()方法有很多类都有,对于String类,它是Java 6.0引入的,

当且仅当String的length()方法返回0时返回true,否则返回false。

比如:"hello".isEmpty()就返回false,而"".isEmpty()返回true。

其他的isEmpty()方法,就像它的名字表示的那样,判断对象内容是否为空,比如集合类里面是否包含元素的。列一个全的吧:

isEmpty() - 类 java.awt.geom.Arc2D.Double 中的方法

确定 RectangularShape 是否为空。

isEmpty() - 类 java.awt.geom.Arc2D.Float 中的方法

确定 RectangularShape 是否为空。

isEmpty() - 类 java.awt.geom.Area 中的方法

测试此 Area 对象是否包括其他区域。

isEmpty() - 类 java.awt.geom.Ellipse2D.Double 中的方法

确定 RectangularShape 是否为空。

isEmpty() - 类 java.awt.geom.Ellipse2D.Float 中的方法

确定 RectangularShape 是否为空。

isEmpty() - 类 java.awt.geom.Rectangle2D.Double 中的方法

确定 RectangularShape 是否为空。

isEmpty() - 类 java.awt.geom.Rectangle2D.Float 中的方法

确定 RectangularShape 是否为空。

isEmpty() - 类 java.awt.geom.RectangularShape 中的方法

确定 RectangularShape 是否为空。

isEmpty() - 类 java.awt.geom.RoundRectangle2D.Double 中的方法

确定 RectangularShape 是否为空。

isEmpty() - 类 java.awt.geom.RoundRectangle2D.Float 中的方法

确定 RectangularShape 是否为空。

isEmpty() - 类 java.awt.Rectangle 中的方法

确定 RectangularShape 是否为空。

isEmpty() - 类 java.awt.RenderingHints 中的方法

如果此 RenderingHints 未包含键-值映射关系,则返回 true。

isEmpty() - 类 java.beans.beancontext.BeanContextSupport 中的方法

报告此 BeanContext 是否为空。

isEmpty() - 类 java.lang.String 中的方法

当且仅当 String.length() 为 0 时返回 true。

isEmpty() - 类 java.util.AbstractCollection 中的方法

如果此 collection 不包含元素,则返回 true。

isEmpty() - 类 java.util.AbstractMap 中的方法

如果此映射未包含键-值映射关系,则返回 true。

isEmpty() - 类 java.util.ArrayDeque 中的方法

如果此双端队列未包含任何元素,则返回 true。

isEmpty() - 类 java.util.ArrayList 中的方法

如果此列表中没有元素,则返回 true

isEmpty() - 类 java.util.BitSet 中的方法

如果此 BitSet 中没有包含任何设置为 true 的位,则返回 ture。

isEmpty() - 接口 java.util.Collection 中的方法

如果此 collection 不包含元素,则返回 true。

isEmpty() - 类 java.util.concurrent.ConcurrentHashMap 中的方法

如果此映射不包含键-值映射关系,则返回 true。

isEmpty() - 类 java.util.concurrent.ConcurrentLinkedQueue 中的方法

如果此队列不包含任何元素,则返回 true。

isEmpty() - 类 java.util.concurrent.ConcurrentSkipListMap 中的方法

如果此映射未包含键-值映射关系,则返回 true。

isEmpty() - 类 java.util.concurrent.ConcurrentSkipListSet 中的方法

如果此 set 不包含任何元素,则返回 true。

isEmpty() - 类 java.util.concurrent.CopyOnWriteArrayList 中的方法

如果此列表不包含任何元素,则返回 true。

isEmpty() - 类 java.util.concurrent.CopyOnWriteArraySet 中的方法

如果此 set 不包含任何元素,则返回 true。

isEmpty() - 类 java.util.concurrent.SynchronousQueue 中的方法

始终返回 true。

isEmpty() - 类 java.util.Dictionary 中的方法

测试此 dictionary 是否不存在从键到值的映射。

isEmpty() - 类 java.util.HashMap 中的方法

如果此映射不包含键-值映射关系,则返回 true。

isEmpty() - 类 java.util.HashSet 中的方法

如果此 set 不包含任何元素,则返回 true。

isEmpty() - 类 java.util.Hashtable 中的方法

测试此哈希表是否没有键映射到值。

isEmpty() - 类 java.util.IdentityHashMap 中的方法

如果此标识哈希映射不包含键-值映射关系,则返回 true。

isEmpty() - 类 java.util.jar.Attributes 中的方法

如果此 Map 不包含属性,则返回 true。

isEmpty() - 接口 java.util.List 中的方法

如果列表不包含元素,则返回 true。

isEmpty() - 接口 java.util.Map 中的方法

如果此映射未包含键-值映射关系,则返回 true。

isEmpty() - 接口 java.util.Set 中的方法

如果 set 不包含元素,则返回 true。

isEmpty() - 类 java.util.TreeSet 中的方法

如果此 set 不包含任何元素,则返回 true。

isEmpty() - 类 java.util.Vector 中的方法

测试此向量是否不包含组件。

isEmpty() - 类 java.util.WeakHashMap 中的方法

如果此映射不包含键-值映射关系,则返回 true。

isEmpty() - 类 javax.activation.MimeTypeParameterList 中的方法

确定此列表是否为空。

isEmpty() - 接口 javax.management.openmbean.TabularData 中的方法

如果此 TabularData 实例中包含的 CompositeData 值的数目(即行数)为零,则返回 true。

isEmpty() - 类 javax.management.openmbean.TabularDataSupport 中的方法

如果此 TabularDataSupport 实例不包含任何行,则返回 true。

isEmpty() - 类 javax.management.timer.Timer 中的方法

测试计时器通知列表是否为空。

isEmpty() - 接口 javax.management.timer.TimerMBean 中的方法

测试计时器通知列表是否为空。

isEmpty() - 类 javax.naming.CompositeName 中的方法

确定此复合名称是否为空。

isEmpty() - 类 javax.naming.CompoundName 中的方法

确定此组合名称是否为空。

isEmpty() - 类 javax.naming.ldap.LdapName 中的方法

确定此 LDAP 名称是否为空。

isEmpty() - 接口 javax.naming.Name 中的方法

确定此名称是否为空。

isEmpty() - 接口 javax.print.attribute.AttributeSet 中的方法

如果此属性集不包含任何属性,则返回 true。

isEmpty() - 类 javax.print.attribute.HashAttributeSet 中的方法

如果此属性集不包含任何属性,则返回 true。

isEmpty() - 类 javax.script.SimpleBindings 中的方法

如果此映射未包含键-值映射关系,则返回 true。

isEmpty() - 类 javax.swing.DefaultListModel 中的方法

测试此列表中是否有组件。

isEmpty() - 类 javax.swing.text.html.parser.Element 中的方法

检查是否为空

isEmpty() - 类 javax.swing.text.SimpleAttributeSet 中的方法

检查属性集是否为空。

java 栈的isEmpty()和empty()有什么区别?

isEmpty是从Vector继承的。 两个方法的用途是一样的。

Stack继承Vector是典型的滥用继承的做法。

Stack的文档推荐使用 Deque ,而不是 Stack

Stack Method

Equivalent Deque Method

push(e)

addFirst(e)

pop()

removeFirst()

peek()

peekFirst()

javaempty的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于、javaempty的信息别忘了在本站进行查找喔。