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 7974413
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T08:21:46+00:00 2026-06-04T08:21:46+00:00

I have a double nested hashmap of hashmaps and want to check for key

  • 0

I have a double nested hashmap of hashmaps and want to check for key existence and place new values. Currently I am nesting if statements to check for key existence at each level. Is there a more efficient way to code this?

HashMap<Foo1, HashMap<Foo2, HashMap<Foo3, Double>>> my_map = new HashMap<Foo1, HashMap<Foo2, HashMap<Foo3, Double>>>();

if (my_map.containsKey(foo1key)) {

    if (my_map.get(foo1key).containsKey(foo2key)) {

        if (my_map.get(foo1key).get(foo2key).containsKey(foo3key)) {

             return my_map.get(foo1key).get(foo2key).get(foo3key);
        }
    }
}

double foo3key = getValue();

// do the above steps again to put foo3key into map.
  • 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-06-04T08:21:47+00:00Added an answer on June 4, 2026 at 8:21 am

    Most efficient way (assuming your values are always non-null) is as follows:

    HashMap<Foo2, HashMap<Foo3, Double>> map2 = my_map.get(foo1Key);
    if(map2!=null) {
      HashMap<Foo3, Double> map3 = map2.get(foo2Key);
      if (map3!=null) {
        Double value = map3.get(foo3Key);
        if (value!=null) {
          return (double)value;
        } else {
          // add value to map3, or whatever
        }
      }
    }
    

    This exploits the following techniques:

    • If get() returns null, you know that the key does not exist (since null values are not allowed)
    • Saving the return value of the previous get for the next lookup, so that you don’t need to chain gets together

    This is all a bit messy though – if you do this kind of manipulation a lot then I would suggest factoring it out into a separate function, so that you can just do:

    double value = getNestedValue(my_map,foo1Key,foo2Key,foo3Key);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a nested map: Map<Integer, Map<Integer, Double>> areaPrices = new HashMap<Integer, Map<Integer, Double>>();
i have a double, the decimal place isn't fix (8-?) i want to fix
I have double values which I would like to convert to String values with
I was wondering if it was faster to have a std::vector<std::vector<double>> where the nested
i have a nested nested (double nested) if statement inside a method which is
Possible Duplicate: How to Break from main/outer loop in a double/nested loop? I have
I have a Postgresql PL/pgSQL function that has a double nested FOR loop, where
So I have a forum application and double nested resources. Section-->Topic-->Replies and are set
I have a NSTextField which is nested by a custom view and I want
Let’s say I have double length that can be either a real length or

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.