3

I would like to know if there is a more efficient way of handling hash maps.

I have the following Map (primary) which is composed of its key and another Map as the value.

   final Map<String,Map<Boolean,String>> primaryMap = new HashMap<>();
     Map<Boolean,String> secondaryMap = new HashMap<>();

For each key I assign 2 secondaryMaps and I inititalize the secondary map again.The sequence is similar to the one displayed below:

 secondaryMap = new HashMap<>();
       secondaryMap.put(true, "A");
       primaryMap.put("DOG", listedMap);

        secondaryMap.put(false, "B");
        primaryMap.put("DOG", listedMap);

 secondaryMap = new HashMap<>();

        secondaryMap.put(true, "C");
        primaryMap.put("CAT", listedMap);

        secondaryMap.put(false, "D");
        primaryMap.put("CAT", listedMap);

Could it be that there is a more efficient way to do this? Does secondaryMap.clear() have an impact in the memory before calling secondaryMap = new HashMap<>();

Many thanks in advance,

Kat

4
  • Are listedMap and secondaryMap supposed to be the same thing? Commented May 29, 2018 at 11:40
  • For sure, you can at least only call primaryMap.put("DOG", listedMap) once (And same for "CAT") Commented May 29, 2018 at 11:42
  • 2
    Each inner map can have at most two elements. Can you simply use a pair of two references? You can add a pair of bools to the node if you'd like to distinguish between missing key and a key mapped to null. Commented May 29, 2018 at 11:42
  • Map<Boolean, String> secondaryMap = primaryMap.computeIfAbsent("DOG", k -> new HashMap<>()); is a nicer way of having maps in maps - and if you want space efficiency you could also use a map of Strings, e.g. map.put("DOG", "AB") and then .getOrDefault("DOG", " ").charAt(boolValue ? 0 : 1) Commented May 29, 2018 at 12:04

2 Answers 2

3

In so far as the inner Map can only have two keys, you could replace it by a custom class :

final Map<String,Map<Boolean,String>> primaryMap = new HashMap<>();

could be :

final Map<String, Foo> map = new HashMap<>();

and you could populate it such as :

 map.put("DOG", new Foo("A", "B"));

where theFoo constructor could be :

public Foo(String valueForTrue, Sting valueForFalse){
   this.valueForTrue = valueForTrue;
   this.valueForFalse = valueForFalse;
}

It will spare some memory (as much less objects will be required) but overall it will make your code much clearer.

Of course if you may add the String value only for the true of the false case you could favor factory over public constructor in Foo such as :

private Foo(){
}

public static Foo ofTrue(String valueForTrue){
   Foo foo = new Foo();
   foo.valueForTrue = valueForTrue;
   return foo;
}

public static Foo ofFalse(String valueForFalse){
   Foo foo = new Foo();
   foo.valueForFalse = valueForFalse;
   return foo;
}

public static Foo of(String valueForTrue, Sting valueForFalse){
   Foo foo = new Foo();
   foo.valueForTrue = valueForTrue;
   foo.valueForFalse = valueForFalse;
   return foo;
}
Sign up to request clarification or add additional context in comments.

5 Comments

Thanks, I guess in order to get the value it would work the same way: for example, map.get("CAT").get(true)
@Katherine99 You are welcome. You could but you could also skip the param with something like map.get("CAT").getTrueValue();
Thanks a lot for this tip!
@davidxxx unless there will be proof for this, I don't think that simply saying that it will take less space is actually accurate. And it might be me here, but I actually like the Map<X, Map<Y,Z>>, again, might be me being weird
@Eugene This way doesn't have the Map object and doesn't have the booleans either. How do you want that it consumes as much as the other way ? "might be me being weird". If you like error prone code, you are maybe not weird but special :)
1

Not sure if this is your code or just a copy/paste problem but it looks like you would be more efficient with:

 secondaryMap = new HashMap<>();
 secondaryMap.put(true, "A");
 secondaryMap.put(false, "B");
 primaryMap.put("DOG", secondaryMap);

And secondaryMap.clear() before your new will also clear the map you put in the primary - they are the same object.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.