Results 1 to 8 of 8
Thread: Help with HashMap?
- 05-02-2012, 08:57 PM #1
Member
- Join Date
- Oct 2011
- Posts
- 30
- Rep Power
- 0
Help with HashMap?
I'm new to HashMaps, and I have read up on them and looked at videos online but i'm still stuck so any comments would be greatly appreciated :)
private HashMap<LibraryItem, LibraryUser> myMap;
(First of all, i'm not sure if this is correct. I want to store LibraryItems & LibraryUsers in this hashMap)
HashMap myMap = new HashMap<LibraryItem, LibraryUser>();
(Constructor to create the new hashMap)
public void storeLibraryItem(LibraryItem LibraryUser)
{
myMap.put(LibraryItem LibraryUser);
}
(I want to store LibraryItems & LibraryUsers in by using this code, but I know it is not right. Would I have 2 separate methods each adding, LibraryItems and the other LibraryUsers?
Or does this work by adding whichever one it is to the hashMap?)
Thanks for any comments, if you need more info just ask ! :D
- 05-02-2012, 09:04 PM #2
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,413
- Blog Entries
- 7
- Rep Power
- 17
Re: Help with HashMap?
This piece of code doesn't make much sense; you have one single parameter named 'LibraryUser' of type LibraryItem. Although allowed, it is confusing to name a variable after a type. In the body of that method you are making another mistake: you are mentioning the type of that variable when you want to put it in your map. It should be something like this:
Note that I named the two parameters 'a' and 'b' arbitrarily; you have to find better names.Java Code:public void storeLibraryItem(LibraryItem a, LibraryUser b) { myMap.put(a, b); }
kind regards,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 05-02-2012, 09:21 PM #3
Member
- Join Date
- Oct 2011
- Posts
- 30
- Rep Power
- 0
Re: Help with HashMap?
Thanks for the reply :)
You say this is of type LibraryItem, as I want the hashMap to store both LibraryUsers & LibraryItems say:Java Code:private HashMap<LibraryItem, LibraryUser> myMap;
LibraryUser of type User
LibraryItem of type Item,
Would it then be best to create two hashMaps?
Previously I had two arrayLists, but now I need to convert them into hashMaps.
e.g :
Then in the constructor :Java Code:private ArrayList<LibraryItem> itemsList; private ArrayList<LibraryUser> usersList;
and finally to add items :Java Code:itemsList = new ArrayList<LibraryItem>(); usersList = new ArrayList<LibraryUser>();
Basically that but HashMaps, Thank you again!Java Code:public void storeLibraryItem(LibraryItem items) { itemsList.add(items); }
- 05-02-2012, 09:40 PM #4
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,413
- Blog Entries
- 7
- Rep Power
- 17
Re: Help with HashMap?
You're making things too complicated; your method in your original post was faulty; did you try my version? (it has two parameters while your version had only one; you need two object to put in a Map, a key and its associated value).
kind regards,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 05-02-2012, 09:51 PM #5
Member
- Join Date
- Oct 2011
- Posts
- 30
- Rep Power
- 0
Re: Help with HashMap?
I get the part that you said thanks :)
But this first line
I think I have done this wrong, I don't want a LibraryUser of type LibraryItem, I basically want a LibraryItem to be of type item (that stores libraryItems, for e.g the title, the author etc..)Java Code:HashMap myMap = new HashMap<LibraryItem, LibraryUser>();
And then seperatly a LibrayrUser to be of type user to store LibraryUser objects.
I did do :
But it is saying that it cannot find the user class. I don't think i'm getting this.Java Code:private HashMap<user, LibraryUser> myMap;
Thanks again for the reply
- 05-02-2012, 09:53 PM #6
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,413
- Blog Entries
- 7
- Rep Power
- 17
Re: Help with HashMap?
The definition of your Map is fine; it is just the definition of your method that is wrong.
kind regards
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 05-02-2012, 09:59 PM #7
Member
- Join Date
- Oct 2011
- Posts
- 30
- Rep Power
- 0
Re: Help with HashMap?
Thankyou :)
It compiles now and I do get it!
One last thing, say I wanted to add a LibraryItem to my map that is called from somewhere else
:Java Code:public void storeLibraryItem(LibraryItem li, LibraryUser lu) { myMap.put(li, lu); }
It is telling me I need both libraryItem & LibraryUser to do this, but I just want to store one for now :/Java Code:storeLibraryItem(li);
thanks again!
- 05-03-2012, 08:36 AM #8
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,413
- Blog Entries
- 7
- Rep Power
- 17
Similar Threads
-
final HashMap hm=new HashMap();
By sangramkeshari.jena in forum New To JavaReplies: 4Last Post: 07-21-2011, 09:44 PM -
Should i use a hashmap?
By 6Sloth9 in forum New To JavaReplies: 7Last Post: 05-02-2011, 02:38 AM -
Hashmap to TXT and TXT to Hashmap
By elvinny in forum Advanced JavaReplies: 4Last Post: 02-16-2011, 11:12 PM -
HashMap Help
By digitol97 in forum New To JavaReplies: 4Last Post: 09-13-2010, 02:38 AM -
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


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks