0

My CS fundamental is slipping away....

How do you store a large number of strings into hashmap/hashtable, so that you can have a lookup time of that string of O(1)?...

Isn't there a java library for hashmap/hashtable?... It seems to store a value to hashmap, you have to store 'key' & 'value'. I assume 'value' is the string I'm trying to store, so what do I have to do sofr the 'key'?

J L
  • 2,157
  • 3
  • 16
  • 15

3 Answers3

1

Java has built-in support; please see the HashMap class.

If you just want values stored, then perhaps a HashSet would be a better choice for when the key is the value.

rgettman
  • 176,041
  • 30
  • 275
  • 357
  • Yes, I know. But I'm just really confused because I cant remember what I am suppose to do if I'm just trying to list of strings into hashtable/hashmap. Am I suppose to be using method Map.put("Hello","Hello"), to store string "Hello" into hashmap? – J L Mar 29 '13 at 00:55
  • Edited my answer to include HashSet. – rgettman Mar 29 '13 at 00:58
0

Isn't there a java library for hashmap/hashtable?

Yes there is. In fact, there are 3 classes in the Java SE library that roughly match your requirements. (Hint: HashSet, HashMap, Hashtable.)

You can find them easily by searching the Javadocs online. Start here - http://docs.oracle.com/javase/7/docs/api/index.html

And if one of those doesn't match your requirements, there are specialist 3rd party libraries ... which you can probably find using Google.


If you are going to be successful as a Java programmer, you need to learn some "search fu" so that you can find these classes for yourself.

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
0

If you need to lookup by key then use HashMap; if you don't and are happy with indexed access then use ArrrayList.

See following for Big-O discussion:

Community
  • 1
  • 1
jarmod
  • 71,565
  • 16
  • 115
  • 122