124

I've been trying to evaluate if this array list is empty or not but none of these have even compiled:

<c:if test="${myObject.featuresList.size == 0 }">                   
<c:if test="${myObject.featuresList.length == 0 }">                 
<c:if test="${myObject.featuresList.size() == 0 }">                 
<c:if test="${myObject.featuresList.length() == 0 }">                   
<c:if test="${myObject.featuresList.empty}">                    
<c:if test="${myObject.featuresList.empty()}">                  
<c:if test="${myObject.featuresList.isEmpty}">  

How can I evaluate if an ArrayList is empty?

OscarRyz
  • 196,001
  • 113
  • 385
  • 569

2 Answers2

255

empty is an operator:

The empty operator is a prefix operation that can be used to determine whether a value is null or empty.

<c:if test="${empty myObject.featuresList}">
informatik01
  • 16,038
  • 10
  • 74
  • 104
bobince
  • 528,062
  • 107
  • 651
  • 834
  • 2
    Although it is documented that the empty operator doesn't play well with Set implementation of Collections in JSTL prior to v2.0 – casey Aug 19 '10 at 20:28
67

There's also the function tags, a bit more flexible:

<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn" %>
<c:if test="${fn:length(list) > 0}">

And here's the tag documentation.

Steve B.
  • 55,454
  • 12
  • 93
  • 132