Results 1 to 9 of 9
  1. #1
    cawendt is offline Member
    Join Date
    Nov 2011
    Posts
    4
    Rep Power
    0

    Unhappy please help me create a hashMap of TreeSets of HogwartsStudent objects

    I just need help getting started by making a HashMap of TreeSets which i have not a clue how to do at all. The keys would be magic house names, and the values will be sorted sets of hogwartsStudent objects.


    Please help, i have no idea what to do.

  2. #2
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,642
    Rep Power
    7

    Default Re: please help me create a hashMap of TreeSets of HogwartsStudent objects


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

    Default Re: please help me create a hashMap of TreeSets of HogwartsStudent objects

    So the keys of your map are of type String (the name of a magic house) and the value would be of type Set (or SortedSet) in which the students are stored. The set would be a Set<Student> or a SortedSet<Student> or, if all you're interested in is the name of a student, a Set<String>. The map would look like this:

    Java Code:
    Map<String, SortedSet<Student>> map= new HashMap<String, SortedSet<Student>>();
    Now think what you have to do if you want to add a tuple, house, student where the key (house) isn't present yet in the map or, alternatively, if the key is already present in the map.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  4. #4
    cawendt is offline Member
    Join Date
    Nov 2011
    Posts
    4
    Rep Power
    0

    Default Re: please help me create a hashMap of TreeSets of HogwartsStudent objects

    If the house isnt present yet....which it isnt i don't think, you would have to create it right?

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

    Default Re: please help me create a hashMap of TreeSets of HogwartsStudent objects

    Quote Originally Posted by cawendt View Post
    If the house isnt present yet....which it isnt i don't think, you would have to create it right?
    Think a bit further: would a house be present in the map if there weren't any students associated with that house? It could be a possibility, but you have to decide.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  6. #6
    cawendt is offline Member
    Join Date
    Nov 2011
    Posts
    4
    Rep Power
    0

    Default Re: please help me create a hashMap of TreeSets of HogwartsStudent objects

    Quote Originally Posted by JosAH View Post
    Think a bit further: would a house be present in the map if there weren't any students associated with that house? It could be a possibility, but you have to decide.

    kind regards,

    Jos



    I know that i have to associate the students which come from an xml file to the house. and the directions say, if it is null create a new treeset and put the student into the map. It says use a outer loop for the keys, and the inner loop for the tree sets.....but i just cant wrap my head around.....now im not looking for a answer just maybe something to head me in the right direction...


    and thank you for your help thus far.

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

    Default Re: please help me create a hashMap of TreeSets of HogwartsStudent objects

    Quote Originally Posted by cawendt View Post
    I know that i have to associate the students which come from an xml file to the house. and the directions say, if it is null create a new treeset and put the student into the map. It says use a outer loop for the keys, and the inner loop for the tree sets.....but i just cant wrap my head around.....now im not looking for a answer just maybe something to head me in the right direction...


    and thank you for your help thus far.
    You're welcome of course; in what way does the data come in? Do you first read a house name, followed by the students of that house? Or do you read house, student tuples? Both can easily be done, but the methods differ a bit; you have to give the details because only you know them ...

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  8. #8
    cawendt is offline Member
    Join Date
    Nov 2011
    Posts
    4
    Rep Power
    0

    Default Re: please help me create a hashMap of TreeSets of HogwartsStudent objects

    Quote Originally Posted by JosAH View Post
    You're welcome of course; in what way does the data come in? Do you first read a house name, followed by the students of that house? Or do you read house, student tuples? Both can easily be done, but the methods differ a bit; you have to give the details because only you know them ...

    kind regards,

    Jos

    In main i start by getting the student list from the xml file, then i have to print out the size. the create a hasSet that will deduplicate the xml file, then create the HashMap of tree sets(which u helped me do), and now is where im stuck. It says get a random magice house(with a method that was giving called randomMagicHouse()). Then it says get the student treeSet associated with the magic house name from the HashMap.(haven't don't that yet...because i don't exactly understand what that means). Then we have to create a new treeSet object if it is null, and put it in the HasMap with the associated magic house name. Then it says attempt to add the student to the TreeSet. And this is all done in a do- while loop. My entire program is finished except this part that im stuck on.

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

    Default Re: please help me create a hashMap of TreeSets of HogwartsStudent objects

    Ok, I'll spoonfeed you a bit; have a look at the following class: it 'manages' the map of houses with their students; you can add a house or you can add a house, student tuple as often as you want. It is sparsely commented to leave something to do for you.

    Java Code:
    class Houses {
    	
    	// all houses and their students
    	private Map<String, Set<String>> houses= new HashMap<String, Set<String>>();
    	
    	// Add a house if it not already exists in the map, return its student set
    	public Set<String> put(String house) {
    		
    		Set<String> students= houses.get(house);
    		
    		if (students == null)
    			houses.put(house, students= new TreeSet<String>());
    		
    		return students;
    	}
    
    	// Add a student to a house (add the house first if it doesn't exist), return true if student was added
    	public boolean put(String house, String student) {
    		
    		Set<String> students= put(house);
    		
    		if( students.contains(student)) return false;
    		
    		students.add(student);
    		return true;
    	}
    	
    	// get all houses with their students
    	Map<String, Set<String>> getHouses() { return houses; }
    }
    Note that I didn't test it so there might be some little irregularities in it.

    kind regards,

    Jos
    Last edited by JosAH; 11-03-2011 at 08:52 AM.
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. Replies: 13
    Last Post: 03-20-2011, 06:46 PM
  2. Replies: 7
    Last Post: 12-08-2009, 07:17 PM
  3. How to create an array of objects
    By redmaverick in forum New To Java
    Replies: 7
    Last Post: 10-19-2009, 02:14 AM
  4. Replies: 1
    Last Post: 01-22-2009, 04:25 PM
  5. HashMap with objects
    By otoro_java in forum New To Java
    Replies: 2
    Last Post: 01-28-2008, 03:28 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
  •