Matthew
Matthew

Reputation: 8426

Why doesn't ArrayList have getSize() instead of size()?

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.

Upvotes: 5

Views: 3591

Answers (3)

Bozho
Bozho

Reputation: 597234

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.

Upvotes: 1

ruakh
ruakh

Reputation: 183466

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.

Upvotes: 11

skaffman
skaffman

Reputation: 403551

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)}

Upvotes: 5

Related Questions