1
public static int preaparePatternForJFugue(ArrayList <String> arrLst) {

    String contents;
    for(int loopIndex2=0;loopIndex2<arrLst.size();loopIndex2++) {
        contents=arrLst.get(loopIndex2);
        contents=contents.replaceAll(",", "Q");
        arrLst.set(loopIndex2, contents);
    }
}

Is there any possibility of optimizing the above code? Code is basically for finding and replacing string within the ArrayList of strings. Do we have any provsion to find and replace within ArrayList directly without getting the strings first and then replacing?

Of cource, we can combine last 3 lines together as below. But this will only save number of code lines I guess.

arrLst.set(loopIndex2, arrLst.get(loopIndex2).replaceAll(",", "Q"));
4
  • 1
    If you want to replace one character with another then one improvement could be using replace(',', 'Q') instead of replaceAll(",", "Q") which will unnecessarily involve regex engine. Also there is no need to name loop index like loopIndex2, simple i is enough. "Is there any possibility of optimizing the above code?" if your code works then you should be asking this question on codereview.stackexchange.com. Commented Sep 13, 2014 at 18:20
  • 2
    A List of immutable objects can't be updated except by the get-change-set paradigm, Commented Sep 13, 2014 at 18:24
  • Laune, So if we use StringBuilder instead of Strings, we could probably do it inline? So method parameter may need to be changed as ArrayList <StringBuilder> arrLst. Not sure if it works. Let me try. Commented Sep 13, 2014 at 18:33
  • 1
    StringBuilder does not have a replace method, so even if you make the list generic a StringBuilder you'll still need to convert it to String. Commented Sep 13, 2014 at 18:44

2 Answers 2

3

You could compile a Pattern and then use a Matcher replaceAll(),

Pattern p = Pattern.compile(",");
for (int loopIndex2 = 0; loopIndex2 < arrLst.size(); loopIndex2++) {
    Matcher matcher = p.matcher(arrLst.get(loopIndex2));
    arrLst.set(loopIndex2, matcher.replaceAll("Q"));
}
Sign up to request clarification or add additional context in comments.

3 Comments

@MaheshaPadyana I believe so, but it might depend on your platform and version.
@ElliottFrisch:This is what replaceAll does anyway.So I don't think there is any difference
@Cratylus Yes, but it compiles the pattern in each loop iteration, this compiles the pattern once.
2

There are various ways to improve this. Of course you would need to decide on what trade offs you can do:

  1. If the strings are small using regexes add too much overhead. You could first check if the character is part of the target and skip it if not with if(!contents.contains(",")){next;} This loops through your string which for small strings it is not bad. If the strings are small you can also scan the string yourself and replace "in place" (not really in place because strings are immutable so you would need to create a new string but you get the idea).
  2. If strings are large enough regexes might be an option so follow the answer of @Elliott Frisch and save the constant creation of the pattern inside the loop.
  3. You did not explain why you are adding the strings with the comma. You could create a wrapper of your list and on add you could strip off unwanted characters before adding them to the list itself. On the same idea you could "mark" the strings with commas by pushing their positions to another data structure e.g. a stack so that in the loop you won't need to go over the entire list but "jump" directly to the elements and replace them. This may help if the list is very big.

Of course you need to test to verify in all cases. If you data set is really small and you don't have real requirements, then write the easiest and most straightforward to understand code.

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.