【问题标题】:Get a String from a Map inside Map Java 8从 Map Java 8 中的 Map 获取字符串
【发布时间】:2018-09-07 18:25:52
【问题描述】:

您认为在另一个地图中的地图中查找值的最佳方法是什么。

    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);

现在,如果我需要“MAP|map2”(在 mapOfMaps 内)和“map2|3”(在 map2 内)的值将是“2.3”

我试图做类似的事情:

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);
 }

但结果是:

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

你能帮我一些想法吗?

【问题讨论】:

  • 为什么不只是map.get(mapfind).get(val)?你当然要检查空值。
  • Map&lt;String, Map&gt; 正在使用 raw Map。不要使用 raw 泛型!!完成定义,即Map&lt;String, Map&lt;String, String&gt;&gt;
  • 而不是地图的地图尝试使用baeldung.com/guava-table

标签: java lambda java-8 hashmap


【解决方案1】:

而不是通过其raw type 声明mapOfMaps,它应该被定义为

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

对应的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;
  }

使用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);
  }

如果我们保留mapOfMaps 的原始类型声明,我们将在getValue 的第一个版本中获得关于unchecked conversion 的类型安全警告,而在第二个版本中,我们需要将结果显式转换为String .由于我们使用mapOfMaps 仅将String 键映射到String 值,因此我们应该相应地声明它。


延伸阅读:What is a raw type and why shouldn't we use it?

【讨论】:

  • 感谢这个答案很好用!!,我更新了原始类型!!
【解决方案2】:

我认为获得所需输出的最简单方法是使用map.get(mapfind).get(val)。但是,如果您想使用现有代码来实现它,您可以在收集到的map 上调用values() 并调用过滤器以获取二级过滤器。下面是你修改方法的代码sn-p

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);
}

【讨论】:

    【解决方案3】:
    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;
    }
    

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-01
    • 2021-09-19
    • 1970-01-01
    • 2021-09-25
    • 2011-11-15
    • 2017-09-23
    相关资源
    最近更新 更多