-2

I'm writing a high throughput java application and need your help settle a debate. The application process strings at a very high frequency and I need to be able to check if the input contains a certain string.

The string is made up of two parts, part 1 and part 2. The two parts are concatenated with a space character.

var data = string1 + " " + string2;

To check if the input belong to my list, I have two approaches:

Approach 1:

Set<String> mylist;

...

void boolean isThere(String part1, String2 part2) {
   return mylist.contains(part1 + " " + part2);
}

Approach 2:


Map<String,Set<String>> mylist;

void boolean isThere(String part1, String2 part2) {
   var partA = mylist.get(part1);
   if (partA != null) {
      return partA.contains(part2);
   }
   return false;
}

The nature of the string parts are short-ish in nature, around 2 to 50 characters each.

Did some benchmarking with some mixed outcomes.

1 Answers1

0

There is going to be no significant difference between HashSet and HashMap because HashSet internally uses HashMap to store data.

Your 2nd approach makes no sense to me because you are storing a Set inside a Map just to check if it contains a string.

contains() method of HashSet has a time complexity of O(1) same as containsKey() of HashMap.

So use a HashSet i.e. your approach 1.

Read this answer.