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

  • Home
  • SEARCH
  • 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 7189067
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T19:13:20+00:00 2026-05-28T19:13:20+00:00

It may be a bad practice, but I haven’t been able to figure out

  • 0

It may be a bad practice, but I haven’t been able to figure out any better solution for my problem. So I have this map

// Map<state, Map<transition, Map<property, value>>>
private Map<String, Map<String, Map<String, String>>> properties;

and I want to initialize it so I don’t get NullPointerException with this

properties.get("a").get("b").get("c");

I tried this one but I didn’t work (obviously)

properties = new HashMap<String, Map<String, Map<String,String>>>();

Other things I tried didn’t compile.

Also if you have any ideas how to avoid this nested maps, I would appreciate it.

  • 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-28T19:13:21+00:00Added an answer on May 28, 2026 at 7:13 pm

    You need to put maps in your maps in your map. Literally:

    properties = new HashMap<String, Map<String, Map<String,String>>>();
    properties.put("a", new HashMap<String, Map<String,String>>());
    properites.get("a").put("b", new HashMap<String,String>());
    

    If your target is lazy initialization without NPE you have to create your own map:

    private static abstract class MyMap<K, V> extends HashMap<K, V> {
        @Override
        public V get(Object key) {
            V val = super.get(key);
            if (val == null && key instanceof K) {
                put((K)key, val = create());
            }
            return val;
        }
    
        protected abstract V create();
    }
    
    
    public void initialize() {
        properties = new MyMap<String, Map<String, Map<String, String>>>() {
            @Override
            protected Map<String, Map<String, String>> create() {
                return new MyMap<String, Map<String, String>>() {
                    @Override
                    protected Map<String, String> create() {
                        return new HashMap<String, String>();
                    }
                };
            }
        };
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

May be obvious but I tried searching and can't figure it out. I am
Is it bad practice using require() a lot? Most websites have a header and
I've done some searching (maybe I'm not describing my problem well enough) but haven't
This may seem like an obvious question but we have a PHP/MySQL app that
Connection.close() may throw SqlException , but I have always assumed that it is safe
May I know if there is any way to remove non-alphabetical symbols from a
May be a stoopid question: I have a number of reference tables in an
May be I am getting old, but I can't find it...
I have been testing the PS3 browser (NetFront) with embedded Flash components on a
Theoretically, the end user should never see internal errors. But in practice, theory and

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.