如何在Java中正确定义多维通用集合?

时间:2017-12-21 18:44:09

标签: java collections hashmap

例如,我想创建一个地图,它是地图地图的地图。所以我已经宣布了地图。从结果集中我想创建一个JSON对象。

error: unable to delete 'feature': remote ref does not exist
error: failed to push some refs to 'https://github.com/MyRepo/Repo.git'

error: src refspec feature does not match any.
error: failed to push some refs to 'https://github.com/MyRepo/Repo.git'

我可以定义为

Map<String, Map<String, Map<String, String>>> data = new LinkedHashMap<String, Map<String, Map<String, String>>>();

但是我需要一次放置一个项目,这样我就可以得到我想要的地图并将其放入。然后相同的下面的迭代,所以我必须将对象转换为地图,留下未经检查的警告。

是否有更好的方式来声明此类集合?

1 个答案:

答案 0 :(得分:4)

您可以使用Google Guava MultiMap,但实际上您最好创建一个新对象并引用单个密钥的整个数据集。

public class MyObjectData {

    private String string1;
    private Map<String, String> map;

    // Getters and setters

}

然后您的地图变为Map<String, MyObjectData>