Evaluate/Check List/Collection Is Empty In JSTL

Evaluate/Check List/Collection Is Empty In JSTL explains about How to evaluate whether a List or Collection is empty using JSTL empty operator.

Consider the example here, a JSP page which contains a HashMap with lot of key value pairs in it and we need check this list is empty or not

For evaluating a Collection is empty or not inside JSTL, we can use empty operator. By using this operator we can check Collection is empty or not

Required Libraries

You need to download

  1. Tomcat 7
  2. JSTL 1.2

Following jar must be in classpath

  1. jstl-1.2.jar

Check List/Collection is empty In JSTL

<!-- Check List/Collection is empty In JSTL -->
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ page import="java.util.HashMap"%>
<%
	HashMap<String, String> numMap = new HashMap<String, String>();
	numMap.put("1", "one");
	numMap.put("2", "two");
	numMap.put("3", "three");
	request.setAttribute("numMap", numMap);
%>
<html>
<body>
    <c:if test="${empty numMap}">
    	Case1 Size is ${numMap.size()}
    </c:if>
    
    <!-- collection is not empty below if condition will be executed -->
    <c:if test="${not empty numMap}">
    	Case2 Size is ${numMap.size()}
    </c:if>
</body>
</html>
Output
Case2 Size is 3





Your email address will not be published. Required fields are marked *