Results 1 to 15 of 15
- 04-25-2012, 01:24 PM #1
Member
- Join Date
- Mar 2012
- Posts
- 88
- Rep Power
- 0
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 likeand it has been populated correctly.XML Code:someMap = new HashMap<String, ArrayList<String>>();
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 ;)Last edited by lannie1980; 04-25-2012 at 01:24 PM. Reason: forgot tags
- 04-25-2012, 02:16 PM #2
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,413
- Blog Entries
- 7
- Rep Power
- 17
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,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 04-25-2012, 02:40 PM #3
Member
- Join Date
- Mar 2012
- Posts
- 88
- Rep Power
- 0
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
- 04-25-2012, 02:54 PM #4
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
- 04-25-2012, 03:03 PM #5
Member
- Join Date
- Mar 2012
- Posts
- 88
- Rep Power
- 0
Re: Extract HashMap values to make a TreeSet
I'll have a look now Tolls cheers ;)
- 04-25-2012, 03:40 PM #6
Member
- Join Date
- Mar 2012
- Posts
- 88
- Rep Power
- 0
Re: Extract HashMap values to make a TreeSet
Ok had a look and got this far
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.XML Code:Collection col = someMap.values(); Set<String> list = new TreeSet<String>();
Can you please help me further with some advice?
Regards
- 04-25-2012, 04:11 PM #7
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
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.Please do not ask for code as refusal often offends.
- 04-25-2012, 07:25 PM #8
Member
- Join Date
- Mar 2012
- Posts
- 88
- Rep Power
- 0
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
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...XML Code:Collection col = someMap.values(); for(ArrayList<String> eachValue : col) { System.out.println(eachValue); }
Best Regards ;)
- 04-25-2012, 08:04 PM #9
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,413
- Blog Entries
- 7
- Rep Power
- 17
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,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 04-25-2012, 08:06 PM #10
Member
- Join Date
- Mar 2012
- Posts
- 88
- Rep Power
- 0
Re: Extract HashMap values to make a TreeSet
scrap that previous post
I now have
which prints out all the Strings. I now need to get them into a TreeSet. Am I along the right lines now?XML Code:for(ArrayList<String> list : someMap.values()) { for(String item : list) { System.out.println(item); }
Regards
- 04-25-2012, 08:10 PM #11
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,413
- Blog Entries
- 7
- Rep Power
- 17
Re: Extract HashMap values to make a TreeSet
When people rob a bank they get a penalty; when banks rob people they get a bonus.
- 04-25-2012, 08:49 PM #12
Member
- Join Date
- Mar 2012
- Posts
- 88
- Rep Power
- 0
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 ;)
- 04-25-2012, 09:30 PM #13
Member
- Join Date
- Mar 2012
- Posts
- 88
- Rep Power
- 0
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.
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.XML Code:for(ArrayList<String> list : someMap.values()) { for(String item : list) { System.out.println(item); }
I tried this
It works in a sort of way but is not what I want - Where am I going wrong now ;(XML Code:for(ArrayList<String> list : someMap.values()) { Set<String> inOrder = new TreeSet<String>(); inOrder.addAll(list); System.out.println(inOrder); }
Best Regards
- 04-25-2012, 09:35 PM #14
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,413
- Blog Entries
- 7
- Rep Power
- 17
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:
kind regards,Java Code:Set inOrder= new TreeSet<String>(); for (ArrayList<String> list : someMap.values()) inOrder.addAll(list); System.out.println(inOrder);
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 04-25-2012, 10:04 PM #15
Member
- Join Date
- Mar 2012
- Posts
- 88
- Rep Power
- 0
Similar Threads
-
use of ajax and jsp to extract values from a table
By sandy1000 in forum JavaServer Pages (JSP) and JSTLReplies: 0Last Post: 12-27-2010, 11:26 AM -
create TreeSet from HashSet and output the values to command window
By sandraW in forum New To JavaReplies: 1Last Post: 08-25-2010, 07:57 PM -
how to extract cmyk values in a pdf file
By jkrishnanvenkat in forum New To JavaReplies: 0Last Post: 01-22-2010, 02:02 PM -
HashMap contains all values but doesn't show all values
By xcallmejudasx in forum New To JavaReplies: 3Last Post: 05-10-2009, 11:35 PM -
How to make a hashmap to allow duplicate values?
By Preethi in forum New To JavaReplies: 0Last Post: 02-08-2008, 12:35 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks