• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Populating HashMaps of HashMap of Objects

 
Omo Jesu
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Rancher
Posts: 1776
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 61
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 10417
63
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic