0

I would like to avoid crating the new instance of SelectItem in side the loop. Could you please help me how can i avoid it.

public List<SelectItem> createLocales() {
    final List<SelectItem> enabledLocales = new ArrayList<SelectItem>();
    final List<String> langCodes = labeldbservice.getEnabledLocales();
    LOGGER.debug("getEnabledLocales: size={0}", langCodes);
    for (final String langCode : langCodes) {
        enabledLocales.add(new SelectItem(langCode, LocaleUtils.toLocale(langCode).getDisplayName()));
    }
    return enabledLocales;
}
jlordo
  • 37,490
  • 6
  • 58
  • 83
Vijay Kumar
  • 81
  • 2
  • 10
  • 3
    Why would you like to avoid it? This seems to be the cleanest way to add a `SelectItem` to `enabledLocales` for each `langCode`. – jlordo Jul 25 '13 at 09:29
  • 3
    Then how would u create different objects for different langCode? – Juned Ahsan Jul 25 '13 at 09:30
  • 1
    possible duplicate of [PMD: Avoid instantiating new objects inside loops](http://stackoverflow.com/questions/17340421/pmd-avoid-instantiating-new-objects-inside-loops) – Andrew Janke Mar 16 '14 at 04:58

3 Answers3

1
public List<SelectItem> createLocales() {
    final List<SelectItem> enabledLocales = new ArrayList<SelectItem>();
    final List<String> langCodes = labeldbservice.getEnabledLocales();
    final SelectItem sItem = new SelectItem();

    LOGGER.debug("getEnabledLocales: size={0}", langCodes);
    for (final String langCode : langCodes) {
        sItem.setValue(langCode);
        sItem.setLabel(LocaleUtils.toLocale(langCode).getDisplayName());
        enabledLocales.add(sItem);
    }
    return enabledLocales;
}
periback2
  • 1,459
  • 4
  • 19
  • 36
Vijay Kumar
  • 81
  • 2
  • 10
1

I solved this PMD Issue by creating a method which will return a new object. I will call this method in loop to get new object.
For your code it would be something like this.

public List<SelectItem> createLocales() {
    final List<SelectItem> enabledLocales = new ArrayList<SelectItem>();
    final List<String> langCodes = labeldbservice.getEnabledLocales();
    LOGGER.debug("getEnabledLocales: size={0}", langCodes);
    for (final String langCode : langCodes) {
        enabledLocales.add(getNewSelectItem(langCode, LocaleUtils.toLocale(langCode).getDisplayName()));
    }
    return enabledLocales;
}

public SelectItem getNewSelectItem(String langCode, String displayName) {
    return new SelectItem(langCode, displayName);
}
0

I do not see why you want to change this code but assuming that you have measured this to be a performance bottleneck, either cache the result of createLocales (globally or per Thread using a ThreadLocal that lazily builds it up) or perhaps instead of returning a list, return a map from langCode to values that lazily instantiate and cache the required SelectItem instances.

boggle
  • 221
  • 3
  • 3
  • initially i had used map only but for retrieval operation at UI side is slow with ma so i am trying to using list interface – Vijay Kumar Jul 25 '13 at 10:36