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());
}
Stuart Marks
  • 127,867
  • 37
  • 205
  • 259
Otha
  • 121
  • 1
  • 1
  • 6
  • I think its a good enough question for https://codereview.stackexchange.com/ – SiddP Apr 27 '17 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? – Eugene Apr 27 '17 at 13:39
  • @Eugene No i may have to apply several pattern on the same line, btw thanks for correct me :) – Otha Apr 27 '17 at 13:43
  • @Otha for each element in the `List` you will have to traverse the entire `Map` then... – Eugene Apr 27 '17 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)`. – Eugene Apr 27 '17 at 14:11
  • ok thats bad , i'll testing this soon, i will mesure time to know if I can use this – Otha Apr 27 '17 at 14:23
  • @Otha maybe duplicated with [this](http://stackoverflow.com/questions/43371521/replaceall-with-java8-lambda-functions) question that you can see all of the solutions & comments, especially Holger's solution & comments. – holi-java Apr 27 '17 at 14:58
  • Well i've tried this morning, all works fine and we dont feel any latency. – Otha Apr 28 '17 at 07:15

0 Answers0