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

    Default Help with using TreeSet or TreeMap to order sets of strings - please

    Hey all,

    I have a small problem that I can't figure out yet. This is my constructor for TeamManager()

    Java Code:
    public TeamManager()
    	{
    		super();
    		arsenalSet = TeamHelper.getArsenalSet();
    		liverpoolSet = TeamHelper.getLiverpoolSet();
    		wiganSet = TeamHelper.getWiganSet();
    		newcastleSet = TeamHelper.getNewcastleSet();
    		evertonSet = TeamHelper.getEvertonSet();
    		teams = new HashMap<String, Set<String>>();
    		teams.put("Arsenal", arsenalSet);
    		teams.put("Liverpool", liverpoolSet);
    		teams.put("Wigan", wiganSet);
    		teams.put("Newcastle", newcastleSet);
    		teams.put("Everton", evertonSet);
        }
    The constructor works fine but what I need to do is to sort the sets of strings(arsenalSet, liverpoolSet etc etc) into alphabetical order of their first names from within 'teams'. These sets of strings contain player names and they are used in other methods within this class.
    This has to be done by making a change to the constructor without making any changes to the declaration of 'teams'.
    What is the correct way to do this? I'm assuming that TreeSet or TreeMap is involved but I'm unsure as to how to implement it.
    Any help is much appreciated.
    Last edited by lannie1980; 03-13-2012 at 08:30 PM.

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

    Default Re: Help with using TreeSet or TreeMap to order sets of strings - please

    You can't sort them within 'teams'.
    The individual Sets can be sorted, but that might depend on the actual Set implementation for arsenalSet, liverpoolSet etc.

    You could sort 'teams' by team name (ie the key) by turning it into a TreeMap, but that wouldn't sort the individual Sets (values).
    Please do not ask for code as refusal often offends.

    ** This space for rent **

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

    Default Re: Help with using TreeSet or TreeMap to order sets of strings - please

    Thanks for the reply Tolls. It looks as though I may have initialized some instance variables wrongly before this constructor and i've also assigned the HashMap to a new object (doh). I'll have a look over it. One day I will be able to do this lol...
    Ye, I can sort the key to order the team names. I was getting all sorts of errors when I tried to TreeMap the values..

    Regards

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

    Default Re: Help with using TreeSet or TreeMap to order sets of strings - please

    The values are a Set, that's why.
    There's no need to Map the data in the Set (you'd need a key for starters).

    If you want them sorted then they'll need to be a TreeSet.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. TreeMap problem
    By Tolls in forum Advanced Java
    Replies: 10
    Last Post: 08-31-2011, 02:58 PM
  2. TreeMap vs Map
    By alpdog14 in forum New To Java
    Replies: 5
    Last Post: 03-27-2011, 06:07 PM
  3. Treemap question
    By dave120 in forum New To Java
    Replies: 0
    Last Post: 10-13-2009, 02:31 AM
  4. Treemap's
    By Russo in forum New To Java
    Replies: 6
    Last Post: 10-11-2009, 01:13 AM
  5. How to use treemap
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-12-2008, 08:47 PM

Tags for this Thread

Posting Permissions

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