0

In the following code

for (UserSelection userSelection : filterList) {
                    String filterName = checkForDataHolder(userSelection
                            .getCriteriaName());
                    csvRow.append(filterName + ":");
                    List<String> list = userSelection.getCriteriaValues();
                    String filterval = "";
                    csvRow.append(DATA_DELIMITER);
                    for (String value : list) {

                        filterval += checkForDataHolder(value) + ", ";
                    }
                    if (filterval.indexOf(",") != -1) {
                        filterval = filterval.substring(0, filterval
                                .lastIndexOf(","));
                    }
                    csvRow.append(DATA_HOLDER + filterval + DATA_HOLDER);
                    csvRow.append(NEW_LINE);

                }

This line filterval += checkForDataHolder(value) + ", ";

causes Concatenation of strings within a loop which creates a StringBuffer for each concatenation. When placed in a loop, this can result in the creation and collection of large numbers of temporary objects. How do I create a StringBuffer before entering the loop, and append to it within the loop ?

Also in the following code

final StringBuffer csvRow = new StringBuffer();
        csvRow.append(NEW_LINE);
        csvRow.append(" ");

I want to Replace the string literal with a character literal to improve performance

consider the following code

public void writeHeader(List<String> headerColList) throws IOException {
        this.header = headerColList;
        String val;
        try {
            final StringBuffer csvRow = new StringBuffer();
            for (int i = 0; i < header.size(); i++) {
                if (i > 0) {
                    csvRow.append(DATA_DELIMITER);
                }
                val = checkForDataHolder(headerColList.get((i)));
                if (!StringUtil.isEmpty(val)) {
                    csvRow.append(DATA_HOLDER + val + DATA_HOLDER);
                }
            }
            if(!isHeaderProcessed) {
                if(this.header != null) {
                    writer.write(csvRow.toString());
                        writer.write(NEW_LINE);
                    }
                    isHeaderProcessed = true;
                }
            } catch (Exception e) {
                log.error(e);
            }
        }

I want to consider catching a more specific class of exception (one or more subclasses of Exception).which is the best option here ?

2 Answers 2

1
for (String value : list) {

                    filterval += checkForDataHolder(value) + ", ";
                }

if (filterval.indexOf(",") != -1) {
                        filterval = filterval.substring(0, filterval
                                .lastIndexOf(","));
                    }

What's going on here? seems to be a very inefficient of taking off the trailing space. Maybe this is faster:

bool Appended = false;
for (String value : list) {
                    if(Appended) { filterval += " ";}
                    filterval += checkForDataHolder(value) + ",";
                    Appended = true;
                }

Things like scanning along a string are more time consuming than using " " instead of ' '.

The exception is really your choice. Are you expecting more than memory allocation errors?

Sign up to request clarification or add additional context in comments.

2 Comments

Not really , I just want to know if just throwing IOException is worh in this case
did the suggestion help with performance? often these things are negligible, but it depends on size of string an how often a function is called. Don't worry about throwing the exception, it's safer than hoping the code went right. Just make sure to catch the error elsewhere and print a useful (for the user) message before exiting
1

How do I create a StringBuffer before entering the loop, and append to it within the loop?

StringBuilder sb = new StringBuilder();
for ..... { }
String filterval = sb.toString();

I want to Replace the string literal with a character literal to improve performance?

So what keeps you from writing ' ' instead of " "? By the way, it wouldn't probably improve performance in any noticeably way. In the case of NEW_LINE, I am not sure if this is a character or a string constant, but note it may be a string constant with a length greater 1, like "\r\n".

I want to consider catching a more specific class of exception (one or more subclasses of Exception).which is the best option here ?

The best option is to catch the exceptions that may actually occur and that you can handle.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.