| Author |
suggetion needed
|
Gopu Akraju
Ranch Hand
Joined: Jan 13, 2008
Posts: 242
|
|
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
Joined: Jan 13, 2008
Posts: 242
|
|
|
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
Joined: Jan 01, 2007
Posts: 184
|
|
|
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
|
The significant problems we face cannot be solved by the same level of thinking which created them – Einstein
SCJP 1.5, SCWCD, SCBCD in the making
|
 |
Gopu Akraju
Ranch Hand
Joined: Jan 13, 2008
Posts: 242
|
|
|
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
Joined: Jan 13, 2008
Posts: 242
|
|
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
Joined: Nov 24, 2005
Posts: 14669
|
|
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.
|
[My Blog]
All roads lead to JavaRanch
|
 |
Gopu Akraju
Ranch Hand
Joined: Jan 13, 2008
Posts: 242
|
|
|
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
Joined: Nov 24, 2005
Posts: 14669
|
|
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.
|
 |
 |
|
|
subject: suggetion needed
|
|
|