Results 1 to 9 of 9
- 11-02-2011, 06:41 PM #1
Member
- Join Date
- Nov 2011
- Posts
- 4
- Rep Power
- 0
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.
- 11-02-2011, 06:57 PM #2
Moderator
- Join Date
- Jul 2010
- Location
- California
- Posts
- 1,604
- Rep Power
- 5
Re: please help me create a hashMap of TreeSets of HogwartsStudent objects
Suggested reading: Lesson: Collection Interfaces (The Java™ Tutorials > Collections)
- 11-02-2011, 07:00 PM #3
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,373
- Blog Entries
- 7
- Rep Power
- 17
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:
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.Java Code:Map<String, SortedSet<Student>> map= new HashMap<String, SortedSet<Student>>();
kind regards,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 11-02-2011, 07:08 PM #4
Member
- Join Date
- Nov 2011
- Posts
- 4
- Rep Power
- 0
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?
- 11-02-2011, 07:19 PM #5
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,373
- Blog Entries
- 7
- Rep Power
- 17
Re: please help me create a hashMap of TreeSets of HogwartsStudent objects
When people rob a bank they get a penalty; when banks rob people they get a bonus.
- 11-02-2011, 07:54 PM #6
Member
- Join Date
- Nov 2011
- Posts
- 4
- Rep Power
- 0
Re: please help me create a hashMap of TreeSets of HogwartsStudent objects
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.
- 11-02-2011, 08:44 PM #7
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,373
- Blog Entries
- 7
- Rep Power
- 17
Re: please help me create a hashMap of TreeSets of HogwartsStudent objects
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,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 11-03-2011, 12:16 AM #8
Member
- Join Date
- Nov 2011
- Posts
- 4
- Rep Power
- 0
Re: please help me create a hashMap of TreeSets of HogwartsStudent objects
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.
- 11-03-2011, 08:12 AM #9
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,373
- Blog Entries
- 7
- Rep Power
- 17
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.
Note that I didn't test it so there might be some little irregularities in it.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; } }
kind regards,
JosLast edited by JosAH; 11-03-2011 at 08:52 AM.
When people rob a bank they get a penalty; when banks rob people they get a bonus.
Similar Threads
-
HashMap treating isomorphic but different objects as the same key
By MartyP in forum Advanced JavaReplies: 13Last Post: 03-20-2011, 06:46 PM -
How to create a new HashMap from a HashMap entries of other methods
By pandeyalok in forum Advanced JavaReplies: 7Last Post: 12-08-2009, 07:17 PM -
How to create an array of objects
By redmaverick in forum New To JavaReplies: 7Last Post: 10-19-2009, 02:14 AM -
read txt file,with some records, create objects and store objects in tables of a db.
By stamv in forum JDBCReplies: 1Last Post: 01-22-2009, 04:25 PM -
HashMap with objects
By otoro_java in forum New To JavaReplies: 2Last Post: 01-28-2008, 03:28 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks