5

I'm making heavy use of ArrayList in some JSP pages. I would like to access an ArrayList like so:

${myArrayList.size}

But since objects must to conform to the JavaBean standard, where myArrayList.getMyPropertyName() is ${myArrayList.myPropertyName} in JSP/JSTL, the myArrayList.size() function is not accessible.

Is there another class I should be using?

Update:

It's a bit difficult to accept that a feature such as getting the size of your ArrayList in JSP is left out just for cleaner one liners but it seems there can be other reasons the designers chose .size() instead of .getSize()

I guess this really isn't a problem if you have support for ${fn:length(myArrayList)} or ${myArrayList.getSize()} syntax.

Cœur
  • 37,241
  • 25
  • 195
  • 267
Matthew
  • 8,183
  • 10
  • 37
  • 65
  • 2
    Because that's the way they wrote it. Use fn:length as per [here](http://stackoverflow.com/questions/3579548/access-the-size-of-a-collection-in-jsp-jstl-el) (and many others). – Dave Newton Jan 07 '12 at 19:04

3 Answers3

11

The title says it all: Why doesn't ArrayList have getSize() instead of size()?

As explained in "Java Collections API Design FAQ":

Why didn't you use "Beans-style names" for consistency?

While the names of the new collections methods do not adhere to the "Beans naming conventions", we believe that they are reasonable, consistent and appropriate to their purpose. It should be remembered that the Beans naming conventions do not apply to the JDK as a whole; the AWT did adopt these conventions, but that decision was somewhat controversial. We suspect that the collections APIs will be used quite pervasively, often with multiple method calls on a single line of code, so it is important that the names be short. Consider, for example, the Iterator methods. Currently, a loop over a collection looks like this:

    for (Iterator i = c.iterator(); i.hasNext(); )
        System.out.println(i.next());
Everything fits neatly on one line, even if the Collection name is a long expression. If we named the methods "getIterator", "hasNextElement" and "getNextElement", this would no longer be the case. Thus, we adopted the "traditional" JDK style rather than the Beans style.
ruakh
  • 175,680
  • 26
  • 273
  • 307
  • 1
    So, no hopes that size() will be deprectated in favor of getSize() then. – Matthew Jan 07 '12 at 19:11
  • 3
    @Matthew It certainly won't be *replaced* with getSize(), but either way, I would suspect your project's timeline would not withstand the time required for a JDK change. – Dave Newton Jan 07 '12 at 19:15
  • 1
    @Matthew: No. Even if everyone were suddenly to agree that `getSize()` were an *a priori* superior name, that would never supersede the "an API is forever" mantra. It would never be worth it to add `getSize()` to the `java.util.List` interface (since that would break all existing implementing classes), and without that change, it would never be worth it to add `getSize()` to `java.util.ArrayList` (since it would only be usable on array-lists represented by expressions having the exact type `ArrayList`). – ruakh Jan 07 '12 at 19:20
  • What about adding a duplicated functionality with a different name getSize(). Althou if you really need this you could probably configure your IDE or some snippets to do this. – Igor Čordaš May 23 '14 at 12:56
  • Addendum: my previous comment is now wrong, because Java has now added a concept of a "default method", so it's now possible for `java.util.List` to declare a `getSize()` method without all existing implementations having to be changed. But even so, I think it's *extremely* unlikely that this will happen. – ruakh Sep 07 '16 at 04:23
5

You use the fn:length JSTL function (see docs).

First, declare the fn namespace at the top of the JSP:

<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>

and then:

${fn:length(myArrayList)}
skaffman
  • 398,947
  • 96
  • 818
  • 769
  • I wish I could use this. I'm working with an older version of Tomcat which isn't compatible. – Matthew Jan 07 '12 at 19:07
  • 1
    @Matthew: Why not? You said you were using JSP EL (e.g. `${myArrayList.size}`), so why not JSTL functions? – skaffman Jan 07 '12 at 19:10
  • @Matthew Then wrap it up in a custom tag, or expose the length from the back end. – Dave Newton Jan 07 '12 at 19:10
  • @Dave Newton Well, I've been doing that as workaround but it doesn't exactly fit in all cases. I thought I would see if there is something I'm missing here - I think what I'm missing here is a more recent version of Tomcat. – Matthew Jan 07 '12 at 19:18
1

The latest version of EL supports method invocations ${collection.size()}

The other two answers give you more details

  • the collections API does not conform to the JavaBeans spec on purpose. Note that the size is not necessarily a property. Some implementations compute it dynamically. Same goes for other methods - they are not simple properties, and making them look like getters would be inappropriate.
  • prior to EL 2.2 you can use ${fn:length(collection)}

By the way, in a JSP you shouldn't normally need the size() of a collection - you just iterate it. If you have to perform some calculations, think of doing that in the servlet/controller. Of course, I agree, it is valid to use that sometimes in the jsp itself.

Bozho
  • 588,226
  • 146
  • 1,060
  • 1,140