Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 551
Matthew Avatar asked Jan 07 '12 18:01

Matthew


1 Answers

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.
like image 151
ruakh Avatar answered Nov 06 '22 12:11

ruakh