Check if a map is empty in Apache Velocity
15,367
isEmpty
and size
are methods, so they should be used like this:
#if ($myMap.isEmpty())
...
#if ($myMap.size() == 0)
![John Farrelly](https://i.stack.imgur.com/gzzb2.jpg?s=256&g=1)
Author by
John Farrelly
Updated on June 21, 2022Comments
-
John Farrelly about 2 years
In my java/spring app, I have a velocity template in which I create a map which will hold values also inserted in the template:
#set ($myMap = {})
What I want to do is have an if/else checking if the map is empty. This doesn't seem to be working for me. I've tried:
#if ($myMap.empty) ... #if ($myMap.size == 0)
Neither of these work. What is the correct way to check if a Map is empty in velocity. I've tried searching the Documentation and SO, but I can't find an example.
-
John Farrelly over 11 yearsThe method should be
isEmpty()
- but once I used them as methods they worked. I had expected that using them as properties would have called the JavaBeans method on them by default. -
GOTO 0 over 11 years@JohnFarrelly You're right! I fixed the answer with the correct method name. Thanks.