扁平化字典

问题描述 投票:0回答:2

给定一个字典 dict,编写一个返回其扁平版本的函数 flattenDictionary 。

input:  dict = {
        "Key1" : "1",
        "Key2" : {
            "a" : "2",
            "b" : "3",
            "c" : {
                "d" : "3",
                "e" : {
                    "" : "1"
                }
            }
        }
    }

output: {
        "Key1" : "1",
        "Key2.a" : "2",
        "Key2.b" : "3",
        "Key2.c.d" : "3",
        "Key2.c.e" : "1"
    }
java hashmap
2个回答
1
投票
class Solution {

 static HashMap<String, String> flattenDictionary(HashMap<String, Object> dict) {
  HashMap<String, String>result = new HashMap<String, String>();
  flattenNesting("", dict, result);

  return result;

 }

    private static void flattenNesting(String parent, Map<String, Object> dict, Map<String, 
 String> result) {
    for (Map.Entry<String, Object> entry : dict.entrySet()) {
        if (entry.getValue() instanceof String) {
            putEntryInResult(parent, result, entry);
        } else {
            flattenNextLevel(parent, result, entry);
        }
    }
}


   private static  void flattenNextLevel(String parent, Map<String, String> result, 
   Map.Entry<String, Object> entry) {
    if (parent.isEmpty()) {
        flattenNesting(entry.getKey(), (Map<String, Object>) entry.getValue(), result);
    } else {
        String key = entry.getKey().isEmpty() ? parent : parent + "." + entry.getKey();
        flattenNesting(key, (Map<String, Object>) entry.getValue(), result);
    }
}


  private static void putEntryInResult(String parent, Map<String, String> result, 
   Map.Entry<String, Object> entry) {
    if (parent.isEmpty()) {
        result.put(entry.getKey(), entry.getValue().toString());
    } else {
        String key = entry.getKey().isEmpty() ? parent : parent + "." + entry.getKey();
        result.put(key, entry.getValue().toString());
    }
}


 public static void main(String[] args) {
    HashMap<String, Object> dict = new HashMap<String, Object>();   
    dict.put("key1", "1");
    HashMap<String, Object> c = new HashMap<String, Object>();
    c.put("d", "3"); c.put("e", "1");
    HashMap<String, Object> key2 = new HashMap<String, Object>();
    key2.put("a", "2"); 
    key2.put("b", "3"); 
    key2.put("c", c);
    dict.put("key2", key2);
  }

}

0
投票

仅使用 1 个辅助(递归)函数即可实现。

public class FlattenDictionary {
    public static void main(String[] args) {
        HashMap<String, Object> dict = new HashMap<String, Object>();
        dict.put("key1", "1");
        HashMap<String, Object> c = new HashMap<String, Object>();
        c.put("d", "3"); c.put("e", "1");
        HashMap<String, Object> key2 = new HashMap<String, Object>();
        key2.put("a", "2");
        key2.put("b", "3");
        key2.put("c", c);
        dict.put("key2", key2);
        HashMap<String, String> result = new HashMap<>();
        helper("", dict, result);
        System.out.println(result);
        Stream stream = result.entrySet().stream();
        System.out.println(stream);
    }

    private static void helper(String initialKey, HashMap<String, Object> dict, Map<String, String> result){
        for(String key: dict.keySet()) {
            Object value = dict.get(key);

            if(value instanceof String) {
                if (initialKey == null || initialKey == "") {
                    result.put(key, String.valueOf(value));
                } else {
                    result.put(initialKey + "." + key, String.valueOf(value));
                }
            } else {
                if(initialKey == null || initialKey == "") {
                    helper(key, (HashMap<String, Object>)value, result);
                } else {
                    helper(initialKey + "." + key,
                            (HashMap<String, Object>)value, result);
                }
            }
        }
    }
© www.soinside.com 2019 - 2024. All rights reserved.