How to hashMap an array in Java?

Java - Stuck using HashMap, please help :)?

  • I'm new to HashMaps, and I hae 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

  • Answer:

    Do it like this : Map<LibraryItem,LibraryUser> myMap = new HashMap<LibraryItem,LibraryUser>(); public void storeLibraryItem(LibraryItem i, LibraryUser u) { myMap.put(i, u); } You almost had it.

Aahmed at Yahoo! Answers Visit the source

Was this solution helpful to you?

Just Added Q & A:

Find solution

For every problem there is a solution! Proved by Solucija.

  • Got an issue and looking for advice?

  • Ask Solucija to search every corner of the Web for help.

  • Get workable solutions and helpful tips in a moment.

Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.