Categories: MSDN / DotNet / Java / Scripts / Linux / PHP Ask - La ask - La Answer

Best data storage method

Hey, iv just got a simple problem what would be the best method for storing this type of data

list of words followed by a list of files the word is contained in e.g

"hello" ["a.txt","b.txt","c.txt"]
"ok" ["b.txt","d.txt"]
"yes" ["a.txt","b.txt"]

would i use hashset -hashmap or is it possible with arraylist

which i wish to add to this list when i find another word contained in a file so if the word is new then it gets added otherwise if i word contained in a file such as "ok" in "f.txt" it becomes

"hello" ["a.txt","b.txt","c.txt"]
"ok" ["b.txt","d.txt","f.txt"]
"yes" ["a.txt","b.txt"]

cheers in advance
[678 byte] By [joesmo] at [2007-11-11 8:16:40]
# 1 Re: Best data storage method
Depends on what you want to manipulate when you look for the "word" - if it is a list of all of the files which contain the word, and you are pre-processing the list, then a "graph" structure of a list of lists is what I would go with. When a new file is found with the word, a node with that file name as a value would be added to the list associated with that word ... now, you can organize the words in a hashlist in which the word is the index generating the hash, and the associated object being stored the list of files with a name - if you are going to find more frequently than you are going to be generating your table ...

If you are adventursome, and want to expand your repertoire, take a look at "tries" ... a perfect use of tries is for a glossary ...
nspils at 2007-11-11 22:35:50 >
# 2 Re: Best data storage method
From what I can gather, you should use a HashMap. use your words as the key and value as a Set/List [use set if you do not want duplicate filenames for the same word and if you need duplicate filenames use a list] of file names in a list.

pseudeocode

String key = "hello"

String value = "sample.txt"

Map mapWords = new HashMap();

if(mapWords.contains(key)){
List listFiles = mapWords.get(key);
listFiles.add(value );
}

else {
List listFiles = new ArrayList();
listFiles.add(value);
mapWords.put(key, listValues);
}
arul at 2007-11-11 22:36:56 >