Wednesday, July 4, 2012

Fastest way to do a lookup on a set of strings in Java

In data integration it is a common requirement to do lookups on sets of strings to see if the string exists in the set. Let's say you have a set of a couple of thousand strings and you want to do a quick lookup to see if a particular string exists in the set, then the fastest way to get that is by storing the set as HashSet and then using the contains() method. As the stackoverflow article mentions Hashsets "... offers constant time performance for the basic operations (add, remove, contains and size)" 



 private static final Set YOUR_SET=Collections.unmodifiableSet(new HashSet(Arrays.asList("VALUE1","VALUE2","VALUE3")));   

No comments:

Post a Comment