Results 1 to 15 of 15
  1. #1
    lannie1980 is offline Member
    Join Date
    Mar 2012
    Posts
    88
    Rep Power
    0

    Default 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
    XML 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 ;)
    Last edited by lannie1980; 04-25-2012 at 01:24 PM. Reason: forgot tags

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,457
    Blog Entries
    7
    Rep Power
    20

    Default 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
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    lannie1980 is offline Member
    Join Date
    Mar 2012
    Posts
    88
    Rep Power
    0

    Default 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

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,967
    Rep Power
    19

    Default 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.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  5. #5
    lannie1980 is offline Member
    Join Date
    Mar 2012
    Posts
    88
    Rep Power
    0

    Default Re: Extract HashMap values to make a TreeSet

    I'll have a look now Tolls cheers ;)

  6. #6
    lannie1980 is offline Member
    Join Date
    Mar 2012
    Posts
    88
    Rep Power
    0

    Default Re: Extract HashMap values to make a TreeSet

    Ok had a look and got this far
    XML 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

  7. #7
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,967
    Rep Power
    19

    Default 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.

    ** This space for rent **

  8. #8
    lannie1980 is offline Member
    Join Date
    Mar 2012
    Posts
    88
    Rep Power
    0

    Default 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

    XML 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 ;)

  9. #9
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,457
    Blog Entries
    7
    Rep Power
    20

    Default 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
    cenosillicaphobia: the fear for an empty beer glass

  10. #10
    lannie1980 is offline Member
    Join Date
    Mar 2012
    Posts
    88
    Rep Power
    0

    Default Re: Extract HashMap values to make a TreeSet

    scrap that previous post

    I now have
    XML 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

  11. #11
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,457
    Blog Entries
    7
    Rep Power
    20

    Default Re: Extract HashMap values to make a TreeSet

    Quote Originally Posted by lannie1980 View Post
    scrap that previous post

    I now have
    XML 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
    cenosillicaphobia: the fear for an empty beer glass

  12. #12
    lannie1980 is offline Member
    Join Date
    Mar 2012
    Posts
    88
    Rep Power
    0

    Default 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 ;)

  13. #13
    lannie1980 is offline Member
    Join Date
    Mar 2012
    Posts
    88
    Rep Power
    0

    Default 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.
    XML 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
    XML 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

  14. #14
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,457
    Blog Entries
    7
    Rep Power
    20

    Default 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:

    Java Code:
    Set inOrder= new TreeSet<String>();
    for (ArrayList<String> list : someMap.values())
       inOrder.addAll(list);
    System.out.println(inOrder);
    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  15. #15
    lannie1980 is offline Member
    Join Date
    Mar 2012
    Posts
    88
    Rep Power
    0

    Default 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
    XML Code:
    Set<String> inOrder = new TreeSet<String>();
    Regards

Similar Threads

  1. use of ajax and jsp to extract values from a table
    By sandy1000 in forum JavaServer Pages (JSP) and JSTL
    Replies: 0
    Last Post: 12-27-2010, 11:26 AM
  2. Replies: 1
    Last Post: 08-25-2010, 07:57 PM
  3. how to extract cmyk values in a pdf file
    By jkrishnanvenkat in forum New To Java
    Replies: 0
    Last Post: 01-22-2010, 02:02 PM
  4. HashMap contains all values but doesn't show all values
    By xcallmejudasx in forum New To Java
    Replies: 3
    Last Post: 05-10-2009, 11:35 PM
  5. How to make a hashmap to allow duplicate values?
    By Preethi in forum New To Java
    Replies: 0
    Last Post: 02-08-2008, 12:35 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •