File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Populating HashMaps of HashMap of Objects Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Populating HashMaps of HashMap of Objects" Watch "Populating HashMaps of HashMap of Objects" New topic
Author

Populating HashMaps of HashMap of Objects

Omo Jesu
Greenhorn

Joined: Nov 30, 2010
Posts: 19
I have a code I am working on but having issues populating the HashMap of HashMaps.. The declaration goes as thus;



Where Game and Store are seperate object classes with only a class variable title.

How do I create instances of the objects in the HashMaps and also populate the two hashmaps because I need to tag an Integer to the game in a particular store... whereas there are different stores and different games in each store.

Thanks in Advance
John Jai
Bartender

Joined: May 31, 2011
Posts: 1776
What is the issue you are facing. You have to create an inner map like below. What is the logic you are trying to achieve?
Omo Jesu
Greenhorn

Joined: Nov 30, 2010
Posts: 19
John Jai wrote:What is the issue you are facing. You have to create an inner map like below. What is the logic you are trying to achieve?



Hi,

Thanks very much for the inner map concept. But I have two classes with a class variable each as a string just to name it as title. I want the map to be able to map an integer value... like the amount in stock to a game in a particular store. Remember that there is a game class and a store class. How will I be able to create instances of a game and store in the HashMap also call/query the object variables.. like the what is the integer for this game in this store...

Thanks
Jim Pouwels
Ranch Hand

Joined: Feb 22, 2012
Posts: 61
Omo Jesu wrote:
John Jai wrote:What is the issue you are facing. You have to create an inner map like below. What is the logic you are trying to achieve?



Hi,

Thanks very much for the inner map concept. But I have two classes with a class variable each as a string just to name it as title. I want the map to be able to map an integer value... like the amount in stock to a game in a particular store. Remember that there is a game class and a store class. How will I be able to create instances of a game and store in the HashMap also call/query the object variables.. like the what is the integer for this game in this store...

Thanks


I think reading the (Hash)Map API will certainly answer your question!
Omo Jesu
Greenhorn

Joined: Nov 30, 2010
Posts: 19
Jim Pouwels wrote:
Omo Jesu wrote:
John Jai wrote:What is the issue you are facing. You have to create an inner map like below. What is the logic you are trying to achieve?



Hi,

Thanks very much for the inner map concept. But I have two classes with a class variable each as a string just to name it as title. I want the map to be able to map an integer value... like the amount in stock to a game in a particular store. Remember that there is a game class and a store class. How will I be able to create instances of a game and store in the HashMap also call/query the object variables.. like the what is the integer for this game in this store...

Thanks


I think reading the (Hash)Map API will certainly answer your question!


I have done that but I am no just getting it.
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7640
    
  19

Omo Jesu wrote:I have done that but I am no just getting it.

So exactly what is it that you're having trouble with?

HashMap relies on the results of the equals() and hashCode() methods for the class you use as a key, so if you intend using Game (and I assume its Game and not Games) and Store as your keys, you'd better get those methods correct; otherwise you'll run into problems.

A TreeMap, on the other hand, can take a Comparator, so you can order those pretty much any way you want.

Another tip: If this Map is going to be used for stockkeeping, I'd use AtomicInteger rather than Integer, because you can change the value. With an Integer you'll have to replace it every time to want to change the value.

Winston
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Populating HashMaps of HashMap of Objects