Solved

Need a container to hold a unique list of strings with a fast lookup

Posted on 2004-07-31
5
224 Views
Last Modified: 2012-05-05
Hi,

I need to maintain a list of unique strings. What container should I use? I need the following requirements:

1) check to see if the string exists in the container (note that it could be a different String object with the same text)
2) must be fast. I mean I don't want the container to be looping through each element doing an equals() call for each element

So I'm thinking it should be some kind of container where the Strings are added in sorted order. This way any kind of equals call can use a divide and conquer approach to testing or something similar.

Are there any containers like this? Whether inside the JDK or freely available somewhere?

Or does anyone have any suggestions the best way to build one?
0
Comment
Question by:ycomp
  • 3
  • 2
5 Comments
 
LVL 7

Accepted Solution

by:
lhankins earned 500 total points
ID: 11686237
You can do this with one of the java.util.Set implementations (either HashSet or TreeSet).

Both implementations are fast with respect to lookup time.   If you want to maintain the items in sorted order (e.g. you want to print them out in sorted order), then use the TreeSet.   Otherwise, use the HashSet.

Example usage :

      Set items = new HashSet();

      items.add("foo");
      items.add("bar");
      items.add("wubba");
      // etc...
     
      if (items.contains("somestring"))
      {
         // item is in the set...
      }


0
 
LVL 7

Expert Comment

by:lhankins
ID: 11686241
BTW - all of the classes listed above are part of the standard JDK (they are in the java.util package).
0
 

Author Comment

by:ycomp
ID: 11686246
ah, so HashSet does use equals() or something to test? I tried it in my code but it didn't work. I guess I was calling it wrong.
0
 

Author Comment

by:ycomp
ID: 11686247
I forgot about hashcodes. It's been a long day.
0
 

Author Comment

by:ycomp
ID: 11686256
yeah was my faulty code that caused the problem
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Weekend adv creator 3 64
Should localization be done inside spring controller 5 36
swing controls 2 21
Netbeans and org.apache.commons.lang3 issue 3 22
After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:

685 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question