Solved

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

Posted on 2004-07-31
5
211 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
I forgot about hashcodes. It's been a long day.
0
 

Author Comment

by:ycomp
Comment Utility
yeah was my faulty code that caused the problem
0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…

728 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now