I want to iterate a map of map in a java 8 way and get the entry which contains 40 in the value in the inner map.

I have tried the following code in order to do that. But I still don't understand whether there is a better way to do this.

import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class MapToMapOperation {

    private Map<String, Map<String, Integer>> mapToMap = new HashMap<>();

    MapToMapOperation() {
        Map<String, Integer> dogMap = new HashMap<>();
        dogMap.put("A", 50);
        dogMap.put("B", 35);
        dogMap.put("D", 40);
        dogMap.put("C", 35);
        mapToMap.put("dog", dogMap);

        Map<String, Integer> catMap = new HashMap<>();
        catMap.put("C", 35);
        catMap.put("D", 40);
        catMap.put("M", 40);
        catMap.put("E", 70);
        catMap.put("H", 35);
        mapToMap.put("cat", catMap);

        Map<String, Integer> donkeyMap = new HashMap<>();
        donkeyMap.put("A", 52);
        donkeyMap.put("E", 70);
        donkeyMap.put("F", 80);
        donkeyMap.put("S", 40);
        donkeyMap.put("G", 90);
        mapToMap.put("donkey", donkeyMap);

    public Map<String, Integer> get(String type) {
        return mapToMap.get(type);

    public List<String> get(Map<String, Integer> map, Integer value) {
        if (!map.containsValue(value)) {
            return Collections.emptyList();
        return keys(map, value).collect(Collectors.toList());

    public Map<String, List<String>> getFullMapBasedOnValue(Integer value) {
        Map<String, List<String>> resultMap = new HashMap<String, List<String>>();
        java.util.Iterator<Entry<String, Map<String, Integer>>> iterator = 
        while (iterator.hasNext()) {
            Map.Entry<String, Map<String, Integer>> entry = iterator.next();
            Map<String, Integer> subMap = entry.getValue();
            if (subMap.containsValue(value)) {
                List<String> list = keys(subMap, value).collect(Collectors.toList());
                resultMap.put(entry.getKey(), list);
        return resultMap;

    public <K, V> Stream<K> keys(Map<K, V> map, V value) {
        return map.entrySet().stream().filter(entry -> value.equals(entry.getValue())).map(Map.Entry::getKey);

    public static void main(String[] args) {
        MapToMapOperation mapOp = new MapToMapOperation();
        mapOp.get("dog").forEach((s, k) -> System.out.println(s + " " + k));

        System.out.println(mapOp.get(mapOp.get("cat"), 35));


  • 1
    \$\begingroup\$ What's the background or the motivation for doing this? Maps aren't designed for reverse lookups, so there's a limit to how good this code can be. \$\endgroup\$ – 200_success Apr 30 '19 at 18:00
  • \$\begingroup\$ Sorry, I dont understand you. \$\endgroup\$ – user10186524 Apr 30 '19 at 18:29
  • 1
    \$\begingroup\$ Can you add a little more explanation of what the code is supposed to do, and how it is doing it? \$\endgroup\$ – 1201ProgramAlarm May 1 '19 at 3:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.