Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7619701
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T03:45:12+00:00 2026-05-31T03:45:12+00:00

I have code I am working on, but I am having issues populating the

  • 0

I have code I am working on, but I am having issues populating the HashMap of HashMaps. The declaration goes as thus;

HashMap<Games, HashMap<Store, Integer>> myMap = new HashMap<Games, HashMap<Store,   Integer>>();

Where Game and Store are separate 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

Edit

Games Class

package gameStore;

public class Games {
    private String title;

    public Games(String inTitle){
        setTitle(inTitle);
    }
    private String getTitle() {
        return title;
    }
    private void setTitle(String title) {
        this.title = title;
    }
}

Stores Class

package gameStore;

public class LocalStores {
    private String nameOfStore;

    public LocalStores(String inNameOfStore){
        setNameOfStore(inNameOfStore);
    }
    private void setNameOfStore(String nameOfStore){
        this.nameOfStore = nameOfStore;
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T03:45:13+00:00Added an answer on May 31, 2026 at 3:45 am

    I would do something like this:

    void addToMap(Games games, Store store, int value) {
        HashMap<Store,Integer> m = myMap.get(games);
        if (m == null) {
            m = new HashMap<Store,Integer>();
            myMap.put(games, m);
        }
        m.put(store, value);
    }
    

    UPDATE:

    Since Games and Store are both used as keys to a HashMap, I would recommand that you add the hashCode and equals methods:

    Games:

    public int hashCode() {
        return title.hashCode();
    }
    
    public boolean equals(Object obj) {
        if (!(obj instanceof Games)) {
            return false;
        }
        Games other = (Games)obj;
        return title.equals(other.title);
    }
    

    LocalStores:

    public int hashCode() {
        return nameOfStore.hashCode();
    }
    
    public boolean equals(Object obj) {
        if (!(obj instanceof LocalStores)) {
            return false;
        }
        LocalStores other = (LocalStores)obj;
        return nameOfStore.equals(other.nameOfStore);
    }
    

    Now, to keep it simple, let’s say that each line of your input file contains three fields separated by tabs: the games’ title, the store’s name, and the integer value. You would read it as follows:

    InputStream stream = new FileInputStream("myfile");
    try {
        Reader reader = new InputStreamReader(stream, "UTF-8"); // or another encoding
        try {
            BufferedInputStream in = new BufferedInputStream(reader);
            try {
                String line = in.readLine();
                while (line != null) {
                    String[] fields = line.split("[\\t]");
                    if (fields.length == 3) {
                        addToMap(new Games(fields[0]), new LocalStores(fields[1]), Integer.parseInt(fields[2]));
                    }
                    line = in.readLine();
                }
            } finally {
                in.close();
            }
        } finally {
            reader.close();
        }
    } finally {
        stream.close();
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

this seems to be weird but it really happened. I have this code working
I'm new to Django but I seem to have nearly identical code working on
I have this working code, but now i need to be able to change
i have implemented code like here but it's not working for iOS 5.0, it
I have jquery validation code which is working fine in ff but the same
I have a codeigniter code that is working on local Apache installation. But it
I have the following code and it's working (as usual) in everything but IE.
i have tried this code to redirect a php page.but it s not working
I have a jQuery code, but need it working by using Mootools: if (
I'm a new code monkey in training, and I'm currently having issues working with

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.