• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

suggetion needed

 
Gopu Akraju
Ranch Hand
Posts: 242
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, I need some suggestion to store my data.

I have my data as follows.

string1 1,2,3,6
string2 3,5,8
string3 9,10,7

These are the set of data which needs to be collected in a container(for example array,hashmap or hashset) and then can be used for further manupulation. Which is the best way to store the data? Thanks.
 
Gopu Akraju
Ranch Hand
Posts: 242
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To be more clear, keys would be string1, string2 etc... and values would be the "1,2,3" or "5,8,10,12" etc
 
abhishek pendkay
Ranch Hand
Posts: 184
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
well if your require the data to be stored as Key-Value pairs then you have to go for some map like hashmap or treemap etc... which map to choose depends on what is the requirment of your application as each of the map implementation has its own benifits
 
Gopu Akraju
Ranch Hand
Posts: 242
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That means, I can have in HashMap, string1 as my key and values can be an array of integers like {1,2,4,5}?
 
Gopu Akraju
Ranch Hand
Posts: 242
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think hashtable would be a better choice as my key value would be a string and integer array as value pair. Thanks.
I tried one small example and it works. But my problem now is it allows multiple same key values. How do I come over as I can not allow such situation in my application. Thanks.
[ March 27, 2008: Message edited by: Gopu Akraju ]
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think hashtable would be a better choice

A HashMap will do the same, except that HashTable is thread-safe. Yor argument does not justify the use of a HashMap

But my problem now is it allows multiple same key values.

What do you mean ? A map cannot contain duplicate keys. If you want to avoid overwriting the same key, you can call the containsKey method to check if the key already exist.
 
Gopu Akraju
Ranch Hand
Posts: 242
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, you are Right. I checked both hash table and hashmap and both are serving the purpose. I may have to remove any key-value pair. In that circumstance, which do youu recomend? Hash table would still a better choice for my case as I can not allow any NULL values. Am I right?
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hash table would still a better choice for my case as I can not allow any NULL values. Am I right?

That's right, but you still have to check for null before setting the value, otherwise you'll get a NullPointerException.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic