principlejavaModerate
String vs List to check if a country is part of a continent
Viewed 0 times
countrycontinentpartlistcheckstring
Problem
I simply need to check if a country is part of a particular continent (only one). The continent has around 50 countries. The list is constant and must be initialized at the time of creation. Which one of the solutions below is a better approach from performance point of view?
-
Having all the counties in a single string and doing simple String.contains()
-
Creating a List and using contains method
I realise the use case is pretty trivial to hamper performance. I feel that
The number of countries won't change and is constant so maintainability and performance over a range of countries shouldn't be considered.
To summarise my question: is the use of a comparatively complex data structure than
-
Having all the counties in a single string and doing simple String.contains()
String countryList = "country1 country2";
boolean isPartOfContinent = countryList.contains("country1");-
Creating a List and using contains method
List countryList = Arrays.asList("country1 ", "country2");
boolean isPartOfContinent = countryList.contains("country1");I realise the use case is pretty trivial to hamper performance. I feel that
List is an overkill for simple stuff like this, whereas my colleague thinks Strings are "meh!" and Java programmers should stick to Lists. What would you use if you had to do this?The number of countries won't change and is constant so maintainability and performance over a range of countries shouldn't be considered.
To summarise my question: is the use of a comparatively complex data structure than
String justifiable when the requirement is so trivial?Solution
Option 1 is sloppy. By that logic, Guinea is a country in Oceania. The end-of-string delimiters are an important part of the data and cannot be discarded like that.
Moreover, option 2 has a chance of being faster: if the first few characters fail to match, it can skip to the next item.
However, I would say that neither implementation is recommended. Better solutions include
-
A simple data structure that allows searching in O(log n) time:
-
A more complicated data structure that can perform lookups in constant time.
Considering options 3 and 4 are not any harder to write than options 1 and 2, you might as well choose the better-performing options.
Moreover, option 2 has a chance of being faster: if the first few characters fail to match, it can skip to the next item.
However, I would say that neither implementation is recommended. Better solutions include
-
A simple data structure that allows searching in O(log n) time:
String[] countries = new String[] { "Countries", "in", "lexicographical", "order" };
boolean isPartOfContinent = (0 <= Arrays.binarySearch(countries, "Country1"));-
A more complicated data structure that can perform lookups in constant time.
Set countries = new HashSet(Arrays.asList(…));
boolean isPartOfContinent = countries.contains("Country1");Considering options 3 and 4 are not any harder to write than options 1 and 2, you might as well choose the better-performing options.
Code Snippets
String[] countries = new String[] { "Countries", "in", "lexicographical", "order" };
boolean isPartOfContinent = (0 <= Arrays.binarySearch(countries, "Country1"));Set<String> countries = new HashSet<String>(Arrays.asList(…));
boolean isPartOfContinent = countries.contains("Country1");Context
StackExchange Code Review Q#73428, answer score: 17
Revisions (0)
No revisions yet.