Java method for judging whether an object is empty of includes null ''

  • 2021-07-24 10:59:23
  • OfStack

In this paper, we share the specific code of Java to judge whether the object is empty for your reference. The specific contents are as follows


package com.gj5u.publics.util;
 
import java.util.List;
 
/**
 *  Judge whether the object is empty or not 
 * 
 * @author Rex
 *
 */
public class EmptyUtil
{
  /**
   *  Judge that the object is empty 
   * 
   * @param obj
   *       Object name 
   * @return  Whether it is empty or not 
   */
  @SuppressWarnings("rawtypes")
  public static boolean isEmpty(Object obj)
  {
    if (obj == null)
    {
      return true;
    }
    if ((obj instanceof List))
    {
      return ((List) obj).size() == 0;
    }
    if ((obj instanceof String))
    {
      return ((String) obj).trim().equals("");
    }
    return false;
  }
  
  /**
   *  Judge that the object is not empty 
   * 
   * @param obj
   *       Object name 
   * @return  Whether it is not empty 
   */
  public static boolean isNotEmpty(Object obj)
  {
    return !isEmpty(obj);
  }
}

Related articles: