Search code examples
javastringbuffer

Should the toString() method be added?


This is the piece of code.

List<BDDObject> childlist = savingObject.getChildren("TherapyAreaReference");

if (childlist.size() > 1) {
  for (int i = 0; i < childlist.size() - 1; i++) {
    String newMedcondRefChild = ((String) childlist
            .get(i)
            .getValue( IDDConstants.IDD_THERAPY_AREA_REF_VALUE))
            .toLowerCase()
            .trim()
            .concat(((String) childlist
            .get(i)
            .getValue(IDDConstants.IDD_THERAPY_AREA_REF_TYPE_NAME)) 
            .toLowerCase().trim());
  }
}

IDDConstants has public static final strings defined in it. As StringBuffer is more effective, how can it be incorporated for the concat operations?


Solution

  • I'm guessing that the intention is to generate a list of 'reports', one for each BDDObject record found. Based on that idea, your code should look more like this:

    public List<String> getReport(List<BDDObject> records) {
    List<String> reports = new ArrayList<String>(record.size());
        for (BDDObject record:records) {
        String newMedcondRefChild = String.valueOf(record.getValue( IDDConstants.IDD_THERAPY_AREA_REF_VALUE))
                .toLowerCase()
                .trim() + String.valueOf(record.getValue(IDDConstants.IDD_THERAPY_AREA_REF_TYPE_NAME))) 
                .toLowerCase().trim());
        reports.add(newMedcondRefChild);
        }
        return reports;
    }
    

    Regarding the question on whether toString() would be helpful, the only place where I see it fitting, would be on the BDDObject itself. It would look something like this:

    class BDDObject {
    ...
        @Override
        public String toString() {
            return String.valueOf(getValue(IDDConstants.IDD_THERAPY_AREA_REF_VALUE)).toLowerCase().trim() + 
                String.valueOf(getValue(IDDConstants.IDD_THERAPY_AREA_REF_TYPE_NAME)).toLowerCase().trim());
    }
    

    In which case, the function to create the report becomes trivial:

    public List<String> getReport(List<BDDObject> records) {
    List<String> reports = new ArrayList<String>(record.size());
        for (BDDObject record:records) {
            reports.add(record.toString());
        }
        return reports;
    }
    

    In case that what you want is a looooong string with all the values concatenated to it, you can use StringBuilder, like this:

    public String getReport(List<BDDObject> records) {
    StringBuilder sb = new StringBuilder();
        for (BDDObject record:records) {
            sb.append(String.valueOf(record.getValue( IDDConstants.IDD_THERAPY_AREA_REF_VALUE))
                .toLowerCase()
                .trim());
            sb.append(String.valueOf(record.getValue(IDDConstants.IDD_THERAPY_AREA_REF_TYPE_NAME)) 
                .toLowerCase().trim()));
    
        }
        return sb.toString();
    }
    

    This will return all the records appended after each other. I doubt its readability, but you I hope you get the idea. StringBuilder is helpful when you need to build a string iteratively (like in the previous example). StringBuilder should not be used to replace single String operations like : String a = b.get() + c.get(); given that the compiler implicitly creates a StringBuilder in these cases and therefore there's no actual performance improvement to be achieved.