1

I have a map which contains regexpr as key and replacer as value and I need to perform a replaceAll for each string in a list.

I should like to know if my answer is correct or if there exists a better way to do that, I'm afraid to see the performance drop. Cause I need to perform this operation on a lot of lists which contain lots of strings.

Map<String,String> m_replace = null; //Map which contains regexpr,replacer

private static String replace(String s) {
    for (Map.Entry<String, String> entry : m_replace.entrySet())
        s = s.replaceAll(entry.getKey(), entry.getValue());
    return s;
}

public List<String> parseList(List<String> input) {
    return input.parallelStream()
                .map(p -> p = replace(p))
                .collect(Collectors.toList());
}
8
  • I think its a good enough question for codereview.stackexchange.com Commented Apr 27, 2017 at 13:00
  • @Otha should you replace just once? I mean once you applied successfully one replaceAll (meaning that the pattern was actually found), shouldn't you stop processing all other map entries? Commented Apr 27, 2017 at 13:39
  • @Eugene No i may have to apply several pattern on the same line, btw thanks for correct me :) Commented Apr 27, 2017 at 13:43
  • @Otha for each element in the List you will have to traverse the entire Map then... Commented Apr 27, 2017 at 14:04
  • @Otha the complexity as far as I can tell is : O(n) for the list times O(n+c) for the Map; where c - is the capacity of the map and n are the number of entries. That would be a total of O(npow2). Commented Apr 27, 2017 at 14:11

0

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.