Results 1 to 8 of 8
  1. #1
    sim18 is offline Member
    Join Date
    Oct 2011
    Posts
    30
    Rep Power
    0

    Default 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

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

    Default Re: Help with HashMap?

    Quote Originally Posted by sim18 View Post
    Java Code:
    private HashMap<LibraryItem, LibraryUser> myMap;
    ...
    public void storeLibraryItem(LibraryItem LibraryUser)
    {
    myMap.put(LibraryItem LibraryUser);
    }
    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:

    Java Code:
    public void storeLibraryItem(LibraryItem a, LibraryUser b)
    {
    myMap.put(a, b);
    }
    Note that I named the two parameters 'a' and 'b' arbitrarily; you have to find better names.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    sim18 is offline Member
    Join Date
    Oct 2011
    Posts
    30
    Rep Power
    0

    Default Re: Help with HashMap?

    Thanks for the reply :)

    Java Code:
     private HashMap<LibraryItem, LibraryUser> myMap;
    You say this is of type LibraryItem, as I want the hashMap to store both LibraryUsers & LibraryItems say:

    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 :

    Java Code:
     
    private ArrayList<LibraryItem> itemsList;
    private ArrayList<LibraryUser> usersList;
    Then in the constructor :
    Java Code:
    itemsList = new ArrayList<LibraryItem>();
    usersList = new ArrayList<LibraryUser>();
    and finally to add items :
    Java Code:
        public void storeLibraryItem(LibraryItem items)
        {
            itemsList.add(items);
        }
    Basically that but HashMaps, Thank you again!

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

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

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  5. #5
    sim18 is offline Member
    Join Date
    Oct 2011
    Posts
    30
    Rep Power
    0

    Default Re: Help with HashMap?

    I get the part that you said thanks :)

    But this first line
    Java Code:
    HashMap myMap = new HashMap<LibraryItem, LibraryUser>();
    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..)

    And then seperatly a LibrayrUser to be of type user to store LibraryUser objects.

    I did do :

    Java Code:
     
    private HashMap<user, LibraryUser> myMap;
    But it is saying that it cannot find the user class. I don't think i'm getting this.

    Thanks again for the reply

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

    Default Re: Help with HashMap?

    The definition of your Map is fine; it is just the definition of your method that is wrong.

    kind regards

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  7. #7
    sim18 is offline Member
    Join Date
    Oct 2011
    Posts
    30
    Rep Power
    0

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

    Java Code:
    storeLibraryItem(li);
    It is telling me I need both libraryItem & LibraryUser to do this, but I just want to store one for now :/

    thanks again!

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

    Default Re: Help with HashMap?

    If your model can handle the absense of a LibraryUser object, you can try this:

    Java Code:
    storeLibraryItem(li, null);
    Jos
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. final HashMap hm=new HashMap();
    By sangramkeshari.jena in forum New To Java
    Replies: 4
    Last Post: 07-21-2011, 10:44 PM
  2. Should i use a hashmap?
    By 6Sloth9 in forum New To Java
    Replies: 7
    Last Post: 05-02-2011, 03:38 AM
  3. Hashmap to TXT and TXT to Hashmap
    By elvinny in forum Advanced Java
    Replies: 4
    Last Post: 02-17-2011, 12:12 AM
  4. HashMap Help
    By digitol97 in forum New To Java
    Replies: 4
    Last Post: 09-13-2010, 03:38 AM
  5. Replies: 7
    Last Post: 12-08-2009, 08:17 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
  •