Extract HashMap values to make a TreeSet
Hi folks,
More help needed I'm afraid - just some pointers to get my brain in gear please.
I have a HashMap that has 4 keys (Strings) that are mapped to values(Strings in an ArrayList).
So it looks like Code:
someMap = new HashMap<String, ArrayList<String>>();
and it has been populated correctly.
My problem is how do I extract the values from the map and then assemble it into a TreeSet (alphabetical order and no duplicates).
Again any help is much appreciated ;)
Re: Extract HashMap values to make a TreeSet
Do you want to store each String from each ArrayList<String> in a TreeSet<String>?
kind regards,
Jos
Re: Extract HashMap values to make a TreeSet
Hi JosAH, thanks for the reply.
Yes, I would like to itemize all the strings in the ArrayList<String> part of the Map(basically the values) in a TreeSet.
Regards
Re: Extract HashMap values to make a TreeSet
Have you looked at the Map API and the Set API?
There's a method on each that, when put together with a loop, will do what you need.
Re: Extract HashMap values to make a TreeSet
I'll have a look now Tolls cheers ;)
Re: Extract HashMap values to make a TreeSet
Ok had a look and got this far
Code:
Collection col = someMap.values();
Set<String> list = new TreeSet<String>();
This code will put all the strings into Collection col. Am I along the right lines here at all? I keep getting comparator errors and other warnings when I try to get the Collection into a TreeSet.
Can you please help me further with some advice?
Regards
Re: Extract HashMap values to make a TreeSet
Nope.
values() will return whatever you defined the values as in someMap.
In your case Collection<ArrayList<String>>.
This is where the loop comes in to get each ArrayList<String>, then use the relevant method on Set to add them all.
Re: Extract HashMap values to make a TreeSet
Hi again, I think I may be overthinking this lol.
What kind of loop would be best
I now have
Code:
Collection col = someMap.values();
for(ArrayList<String> eachValue : col)
{
System.out.println(eachValue);
}
which is wrong i know - i can't seem to figure this one out ;( Could someone tell me the methods to use lol and i'll even figure out the loop for myself...
Best Regards ;)
Re: Extract HashMap values to make a TreeSet
So 'eachValue' is an ArrayList<String>, you have that right; now look for the API documentation for the Set interface, it has a addAll( ... ) method that can add all values of an arbitrary collection (such as an ArrayList) to the set. You should actually bookmark or download the entire API documentation because every Java programmer needs it more often than not.
kind regards,
Jos
Re: Extract HashMap values to make a TreeSet
scrap that previous post
I now have
Code:
for(ArrayList<String> list : someMap.values())
{
for(String item : list)
{
System.out.println(item);
}
which prints out all the Strings. I now need to get them into a TreeSet. Am I along the right lines now?
Regards
Re: Extract HashMap values to make a TreeSet
Quote:
Originally Posted by
lannie1980
scrap that previous post
I now have
Code:
for(ArrayList<String> list : someMap.values())
{
for(String item : list)
{
System.out.println(item);
}
which prints out all the Strings. I now need to get them into a TreeSet. Am I along the right lines now?
Regards
Yep, but reread my previous reply: you don't need that nested loop, i.e. a Set can add a bunch of values (in a Collection) all with one method.
kind regards,
Jos
Re: Extract HashMap values to make a TreeSet
cool - i'll try and get it done in one method now - Thanks for the advice on the API, downloading it now - i'm sure it will be invaluable.
Regards ;)
Re: Extract HashMap values to make a TreeSet
Hi
My earlier code with nested loop prints the strings in the arraylist line by line - which is halfway there.
Code:
for(ArrayList<String> list : someMap.values())
{
for(String item : list)
{
System.out.println(item);
}
I need the method to store these strings the way they are above but in a TreeSet so that any duplicate items are taken out and they are printed in alphabetical order.
I tried this
Code:
for(ArrayList<String> list : someMap.values())
{
Set<String> inOrder = new TreeSet<String>();
inOrder.addAll(list);
System.out.println(inOrder);
}
It works in a sort of way but is not what I want - Where am I going wrong now ;(
Best Regards
Re: Extract HashMap values to make a TreeSet
You want to add the elements from each List to your single Set. Why are you defining a new Set each time the loop body executes? I'd expected something like this:
Code:
Set inOrder= new TreeSet<String>();
for (ArrayList<String> list : someMap.values())
inOrder.addAll(list);
System.out.println(inOrder);
kind regards,
Jos
Re: Extract HashMap values to make a TreeSet
God dammit lol - I have no idea why I put the new Set inside the loop - it was a guess really ;( Thanks for all your help JosAH - I can understand a bit more now.
1st line should be Code:
Set<String> inOrder = new TreeSet<String>();
Regards :(clap):