首页 > 解决方案 > 如何在java中声明字典字典

问题描述

我有 C#.Net 代码,它调用在 JAVA Ecllips 开普勒中创建的 SOAP 服务。从 Web 服务公开的 API(函数)应将 C#.NET 代码中的数据作为参数。我需要发送到 API 的数据是 c#.NET 中的字典字典,如下所示:

Dictionary<string, Dictionary<string, string>> LinksCollection = null;

这种结构如何在 Java 中实现?

我搜索和尝试的内容:

1]以下是在java中实现字典的方法:

1]HashMap

    eg:Map<String, String> map = new HashMap<String, String>();

2]LinkedHashMap
3]Hashtable  

     eg: Dictionary d = new Hashtable();

Is it the right way to implement as follows ?   

 

    Declaring Dictionary: 
       Map<String, String> mapA = new HashMap<String, String>();
     Declaring Dictionary of Dictionary:  
       Map<String, mapA> map = new HashMap<String, mapA>();

标签: javadictionary

解决方案


添加到 Cauis Jard 的答案中,没有一种“最佳”类型可以在所有情况下使用。

  • 如果您不关心订购,那么HashMap是最明显的选择。这可能是您的“默认”选择......如果线程安全不是问题。

  • 如果要保留插入顺序,请使用LinkedHashMap.

  • 如果要按排序顺序迭代字典键,请使用TreeMap.

  • 如果要从多个线程中使用字典,请根据您的要求从以下选项中进行选择:

    • ConcurrentHashMap
    • SkipListMap
    • Collections.synchronizedMap(...)
    • Collections.unmodifiableMap(...)

您还可以使用Properties甚至是遗留/过时的HashtableDictionary类。


推荐阅读