Converting and enriching an array of data The method below converts an array of TheatreListData objects to an array of TheatreListDataWrapper. It also loads additional attributes such as patientIdentifierNumber and requestText to enrich the converted data.

This method is called by many EJBs, and so I'm especially looking for ways to improve performance.

/** 
* Converts TheatreListDatas into TheatreListDataWrappers. This also 
* loads the attributes that has to be generated from client such as 
* patientIdentifierNumber, requestText 
* 
* @param theatreListData 
* @return TheatreListDataWrapper[] 
*/
public TheatreListDataWrapper[] wrapData(TheatreListData[] theatreListData){

    TheatreListDataWrapper[] wrapperList = wrapDataWithoutRequestData(theatreListData);

    if (wrapperList != null &amp;&amp; wrapperList.length &gt; 0)
    {
      Set&lt;String&gt; requestIds = new HashSet&lt;String&gt;();

      for (TheatreListData listData : theatreListData)
      {

        // Collect request id for each List Data into a set
        String requestId = listData.getCarePlan().requestId;
        //Should check for '-1' here to avoid calls to fetch invalid requests.
        //'-1' could have been saved with registrations with 'None' requests.
        if (requestId != null &amp;&amp; requestId.trim().length() &gt; 0 &amp;&amp; !requestId.equals("-1"))
        {
          //Since adding to a set no duplicates are possible.
          requestIds.add(requestId);
        }
      }

      //Load request Id, request Text map using the collected request ids.
      Map&lt;String, String&gt; requestMap = Collections.emptyMap();
      if (!requestIds.isEmpty())
      {
        requestMap = getRequestDelegator().getRequestPresentationStringsByIds(new ArrayList&lt;String&gt;(requestIds));
      }

      //Create wrappers &amp; fill with loaded data.
      for (int i = 0; i &lt; theatreListData.length; i++)
      {
        TheatreListDataWrapper wrapper = wrapperList[i];

        TheatreListData listData = theatreListData[i];
        String requestId = listData.getCarePlan().requestId;
        if (requestId != null &amp;&amp; requestId.trim().length() &gt; 0)
        {
          wrapper.requestText = requestMap.get(requestId) != null ? requestMap.get(requestId) : "";
        }

        wrapperList[i] = wrapper;
      }

    }
    return wrapperList;

  }
Depending on the constant overhead of retrieving the request presentation string for a request ID (i.e. is it considerably more efficient to get all the strings for a bunch of IDs at once than for each ID separately?), a better design might be to iterate over the theatre list only once, getting the representation strings as you go and caching the results in a Map.

Map&lt;String, String&gt; cache = new HashMap&lt;&gt;();
for (int i = 0; i &lt; theatreListData.length; i++) {
    TheatreListDataWrapper wrapper = wrapperList[i];
    TheatreListData listData = theatreListData[i];
    String requestId = listData.getCarePlan().requestId;
    if (validRequestId(requestId)) {
        String text = cache.get(requestId);
        if (text == null) {
            text = getRequestDelegator().getRequestPresentationStringsById(requestId);
            cache.put(requestId, text);
        }
        wrapper.requestText = text;
    }
}


This is probably not a good idea of getting the request text requires, say, a database query.

Other than that, I can think of no algorithmic improvements. You should store the result of requestMap.get(requestId) in a local variable so you don't do the call twice; that's inefficient. Perhaps you can modify getRequestPresentationStringsByIds to take a Collection instead of a List so that you don't have to wrap your set in an ArrayList. Can't think of more.