Skip to main content

Faster and Faster with Java

Posted by larryjava on April 14, 2013 at 12:19 PM PDT

Tuning Java code presents many challenges and addresses several goals.
One of these goals is to make the application "run faster".
Recently, our end-users were getting multiple-second response times from our web application's home page.
This was unacceptable.

We were asked to see what we can do to make the web application "run faster".
We began by looking at the usual suspects... database access, network bandwidth, authentication & authorization.
We asked the usual questions... "Do we need a faster database, faster network, faster login process?"

Surprisingly, we identified some suspect code that was being executed by the home page.
All this code did was a text string search/match within an array list object.

The original code looped thru the array list, retrieved an object,
retrieved a string from the object, and then checked for a match.

if (arrayListOfURLObjects != null && arrayListOfURLObjects.size() > 0) {
for (int count = 0; count < arrayListOfURLObjects.size(); count++) {
urlObjectFromArrayList = (URLObject) arrayListOfURLObjects.get(count);
if (urlObjectFromArrayList != null) {
urlStringFromObject = urlObjectFromArrayList.getUrlStringFromObject();
if (urlStringFromObject != null && !"".equals(urlStringFromObject)) {
if (urlStringToMatchTo.equals(urlStringFromObject)) {
urlMatchFoundFlag = true;
if (urlMatchFoundFlag) {

This code was also executed multiple times by the home page.

So we revised the code to use a hash set of strings (instead of an array list) to check for a match.

if (hashSetOfURLStrings != null && hashSetOfURLStrings.size() > 0) {
if (hashSetOfURLStrings.contains(urlStringToMatchTo)){
urlMatchFoundFlag = true;

The revised code resulted in a huge improvement in the home page's response time...
from multi-second to sub-second response time! Wow!
Our end-users were very happy with these results.
Minor changes, big results ... we were very happy too.

Despite one's best intentions, don't try to do yourself what Java can do better.
Lesson learned. Java saves the day!