• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Populating HashMaps of HashMap of Objects

 
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Rancher
Posts: 1776
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Wait for it ... wait .... wait .... NOW! Pafiffle! A perfect tiny ad!
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic