简体   繁体   English

从Map Java 8中的Map获取字符串

[英]Get a String from a Map inside Map Java 8

What do you think is the best way to find a value in a map inside another map. 您认为什么是在另一张地图中的一张地图中查找值的最佳方法。

    Map <String, String> map1 = new HashMap<>();

    map1.put("map1|1", "1.1");
    map1.put("map1|2", "1.2");
    map1.put("map1|3", "1.3");
    map1.put("map1|4", "1.4");

    Map <String, String> map2 = new HashMap<>();

    map2.put("map2|1", "2.1");
    map2.put("map2|2", "2.2");
    map2.put("map2|3", "2.3");
    map2.put("map2|4", "2.4");


    Map<String, Map> mapOfMaps = new HashMap<>();

    mapOfMaps.put("MAP|map1", map1);
    mapOfMaps.put("MAP|map2", map2);

Now if I need the value of "MAP|map2" (inside mapOfMaps) and "map2|3" (inside map2) will be "2.3" 现在,如果我需要“ MAP | map2”(在mapOfMaps内)和“ map2 | 3”(在map2内)的值将为“ 2.3”

I tried to do something like: 我试图做类似的事情:

System.out.println("x="+getValue(mapOfMaps,"MAP|map2", "map2|4"));

public static String getValue (Map<String, Map> map,String mapfind, String val) {

     Map<Object, Object> mp = map.entrySet().stream()
                .filter(x -> x.getKey().equals(mapfind))
                .collect(Collectors.toMap(p -> p.getKey(), p -> p.getValue()));

     System.out.println("--------"+mp);

     return (String) mp.get(val);
 }

but the result is: 但结果是:

--------{MAP|map2={map2|1=2.1, map2|4=2.4, map2|2=2.2, map2|3=2.3}}
x=null

Can you help me with some ideas? 你能帮我一些想法吗?

Instead of declaring mapOfMaps by its raw type it should be defined as 与其通过原始类型声明mapOfMaps ,不如将其定义为

Map<String, Map<String, String>> mapOfMaps = new HashMap<>();

The corresponding getValue method would look like this: 相应的getValue方法如下所示:

  public static String getValue(Map<String, Map<String, String>> mapOfMaps, String mapfind, String val) {
    Map<String, String> innerMap = mapOfMaps.get(mapfind);
    return innerMap != null ?
      innerMap.get(val) :
      null;
  }

Using Optional we can write it as follows: 使用Optional我们可以这样写:

  public static String getValue(Map<String, Map<String, String>> mapOfMaps, String mapfind, String val) {
    return Optional.ofNullable(mapOfMaps.get(mapfind))
      .map(m -> m.get(val))
      .orElse(null);
  }

If we kept mapOfMaps declared by its raw type we would get in the first version of getValue a type safety warning about an unchecked conversion and in the second version we would need explicitly cast the result to String . 如果保留由其原始类型声明的mapOfMaps ,则将在getValue的第一个版本中获得有关未经检查的转换的类型安全警告,而在第二个版本中,我们需要将结果显式转换String Since we use mapOfMaps only to map String keys to String values we should declare it accordingly. 由于我们仅使用mapOfMapsString键映射到String值,因此我们应该相应地声明它。


Further reading: What is a raw type and why shouldn't we use it? 进一步阅读: 什么是原始类型,为什么我们不应该使用它?

I think the easiest way to get your desired output would be to use map.get(mapfind).get(val) . 我认为获得所需输出的最简单方法是使用map.get(mapfind).get(val) But if you want to achieve it using your existing code, you could call values() on the collected map and call filter to get a second level filter. 但是,如果您想使用现有代码实现此目标,则可以在收集的map上调用values()并调用filter获得第二级过滤器。 Below is the code snippet of your modified method 以下是修改后的方法的代码段

public static String getValue(Map<String, Map> map, String mapfind, String val) {
    Map mp = map.entrySet().stream().filter(x -> x.getKey().equals(mapfind))
            .collect(Collectors.toMap(p -> p.getKey(), p -> p.getValue()))

            .values().stream().filter(y -> y.containsKey(val)).findAny().orElse(null);

    System.out.println("--------" + mp);
    if (mp == null)
        return "";
    return (String) mp.get(val);
}
public static String getValue (Map<String, Map> map,String mapfind, String val) {
    Map childMap = map.get(mapfind);
    if (childMap == null) {
        return null;
    }
    return childMap.containsKey(val) ? childMap.get(val).toString() : null;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM