简体   繁体   English

访问地图的最佳方法

[英]Best way to access a Map of Map

Consider the following situation: 请考虑以下情况:

I have a map with String as a key and another Map as a value. 我有一个使用String作为键的映射和另一个Map作为值的映射。 Now the Map in the value is also a Map with a String as key and a Map as value. 现在,值中的Map也是一个以String为键和Map为值的Map。 We have some n levels like this and the final level has a Map with String as a key and String as a value. 我们有这样的n个级别,最后一个级别有一个Map,其中String为键,String为值。 Now suppose, I have the list of keys for each level. 现在假设,我有每个级别的键列表。 I have to get the final String value. 我必须获取最终的String值。

This is how I have thought to implement it in java: 这就是我想在Java中实现它的方式:

Object q = initialMap; 
for(String key : keyList)
{
    Map x = (Map)q;
    q = x.get(key);
}
return (String)q;

Is this the right way to approach this? 这是解决这个问题的正确方法吗? I get the unchecked conversion warning with this. 我得到了未经检查的转换警告。 Is there any better way of doing this? 有什么更好的方法吗?

I think I understand you problem, you obtain from a file a Map of String to Map of String to Map of ... etc. Something like this: 我想我理解您的问题,您从文件中获取字符串映射到字符串映射到...等的映射。类似这样的内容:

 Map<String, Map<String, Map<String, Map<String, String>>>> initialMap;

What is not clear in your question is if you know how many levels of map there will be. 您的问题中尚不清楚的是,您是否知道将要映射多少个级别。 If yes, then just write your initialMap like I just did (for 4 levels of Maps) for the number of levels you need. 如果是的话,那么像我刚才所做的那样(针对4个级别的地图)编写您所需的initialMap即可。

If you don't know by advance how much levels you have then it's more difficult. 如果您事先不知道自己有多少关卡,那就更困难了。 In that case, your algorithm is sound. 在这种情况下,您的算法是正确的。 I would only make it more generics like this: 我只会像这样使它更通用:

Object q = initialMap; 
for(String key : keyList)
{
    Map<String, ?> x = (Map<String, ?>)q;
    q = x.get(key);
}
return (String)q;

But this will always give you a warning, that you will have to suppress with adding @SuppressWarnings("unchecked") to the method. 但这总是会给您一个警告,您必须在该方法中添加@SuppressWarnings(“ unchecked”)来取消该警告。

You can try abstracting this away in a method. 您可以尝试通过一种方法将其抽象化。

Edit : there is a way to actually abstract it completely in utility classes. 编辑 :有一种方法可以在实用程序类中完全完全抽象它。 I would call what you want a kind of recursive Map. 我会称呼您想要的一种递归Map。 Any of your Map is either a map from String to String or map to String to a recursive map. 您的任何Map要么是从String到String的映射,要么是从String到递归映射的映射。 This can be abstracted this way: 可以这样抽象:

interface IRecursiveMap {
  String get(List<String> keyList);
}

class RecursiveMap implements IRecursiveMap{
  Map<String, IRecursiveMap> map;

  @Override
  public String get(List<String> keyList) {
    String key = keyList.get(0);
    return map.get(key).get(keyList.subList(1, keyList.size()));
  }
}

class ValueMap implements IRecursiveMap{
  Map<String, String> map;

  @Override
  public String get(List<String> keyList) {
    String key = keyList.get(0);
    return map.get(key);
  }
}

So IRecursiveMap is the interface that defines the common behaviour of your maps: the ability to get something from a list of keys. 因此,IRecursiveMap是定义地图常见行为的接口:从键列表中获取内容的功能。 RecursiveMap is the implementation that maps a String to another IRecursiveMap and ValueMap is the last map of the recursion, which actually maps String to String. RecursiveMap是将String映射到另一个IRecursiveMap的实现,而ValueMap是递归的最后一个映射,它实际上将String映射到String。

The good in that implementation is : 该实现的优点是:

  • The code use generics properly and the data is fully typed. 该代码正确使用泛型,并且数据已完全键入。 It means you don't have any more generic warnings. 这意味着您不再有一般警告。
  • All the logic to store stuff in this chain of maps is abstracted in a few utility classes. 在这套地图链中存储东西的所有逻辑在一些实用程序类中是抽象的。

On the other hand the downside is that it may be a little complicated to fill your map, but again it can be done recursively. 另一方面,不利的一面是,填满地图可能有些复杂,但可以再次递归进行。

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

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