I have this HashMap containing words and the count for each word from a given text file;
{word=1, word2=1, word3=2, word4=1, word5=1, word6=4, word7=1, word8=1};
i was following your suggestion in other topics; but i have notice that if use fro example sorted Collections and i search for a specific KEY which could be 1 in this case it only return me one word while instead can return more values for same key;
the point is between all the collections:
Lists Maps ArrayLists Trees HashMaps HashTables
which is the most advisable to use?
on my Class the user will input an int and that int will correspond to the 1st or 2nd or 3rd or 4th and so on..... words used in the files base on the count and occurences;
it's challenging
so far i have managed to store in hashmap and eventually order it in a Tree by Desc Key; so first element will be the greater; but still the algorithm needs more sense;
ps. i do not expect solution or pieces of codes but a good input to start ... a very good advise or direction best to follow;
Maps, by nature, store (will return) only one element per key. That means that if you'll store
[key:1, val:a]
and then store again[key:1, val:2]
. The second insertion will override the first and when you'll "get"key:1
the returned result will beb
.You can, however, to store a List per key. This list can store all the value values per the same key. So we'll declare the map that we'l use as follows:
This is how insert should look like:
the "get" is pretty straightforward:
you get the list of values and if it's not null - iterate the values and print/do whatever you want with them.