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
listedMapandsecondaryMapsupposed to be the same thing?null.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)