Reputation: 19
I have simple collection of string objects might be around 10 elements , but i use this collection in production environment such that the we search for a given string in that collection millions of tiimes , what is the best collection or data structure we can use to get the best results so that seach operation can be performed in 0(1) time we can use HashMap here but the order of search there is in constant time not 0(1) i want to make sure that search is 0(1).
Our data structure must return true if present , else false if not present
Upvotes: 0
Views: 821
Reputation: 198221
Constant time is O(1). HashMap
is fine. (Or HashSet
, depending on whether you need a Set
or a Map
.)
If your set is immutable, Guava's ImmutableSet
will reduce memory footprint by a factor of ~3 (and probably give you a small constant factor of improved speed).
Upvotes: 1
Reputation: 189
If you can't use HashSet/HashMap as previously suggested, you could write a Radix Tree implementation.
Upvotes: 0
Reputation: 114807
Use a HashSet<String>
structure. The contains()
operation has a complexity of O(1).
Upvotes: 5