2009-08-13 62 views

回答

343

Dictionary可能是最接近的。 System.Collections.Generic.Dictionary实现了System.Collections.Generic.IDictionary接口(它类似于Java的Map接口)。

一些显着的差别,你应该知道的:

  • 添加/获取项目
    • Java的HashMap具有putget方法设置/获取项目
      • myMap.put(key, value)
      • MyObject value = myMap.get(key)
    • C#的字典使用[]索引用于设置/获取项目
      • myDictionary[key] = value
      • MyObject value = myDictionary[key]
  • null
    • Java的HashMap允许空关键小号
    • .NET的Dictionary如果您尝试添加一个空键
  • 添加重复键
    • Java的HashMap将替换为新一个现有的值抛出ArgumentNullException
    • 如果您使用[]索引,.NET的Dictionary将用新值替换现有值。如果您使用Add方法,则会抛出ArgumentException
  • 试图获取一个不存在的关键
    • Java的HashMap将返回null。
    • .NET的Dictionary将抛出一个KeyNotFoundException。您可以使用TryGetValue方法,而不是[]索引,以避免这种情况:
      MyObject value = null; if (!myDictionary.TryGetValue(key, value)) { /* key doesn't exist */ }

Dictionary的有ContainsKey方法,可以帮助解决前两个问题。

+7

没有一个确切的等价物(在JAVA中,HashMap允许空值和null键)http://download.oracle.com/javase/1.4.2/docs/api/java/util/HashMap.html – 2010-12-25 18:55:56

+3

是的,词典很接近但不完全确切。 – Powerlord 2010-12-25 21:00:37

+9

注意,'Dictionary'在添加重复键时抛出异常。 – 2011-12-27 20:08:46

4

请查阅MSDN上Hashtable类的文档。

表示根据密钥的哈希码组织的键 - 值对的集合。

另外,请记住,这不是线程安全的。

+20

'Dictionary '是最好的,因为编译时类型检查,因为它不需要装箱值类型。 – Thorarin 2009-08-13 16:56:29

34

C# equivalent to Java HashMap

我需要一个解释,其接受一个“空”键,但似乎没有一个本地人,所以我写我自己。其实很简单。我从字典继承,添加一个专用字段来保存“空”键的值,然后覆盖索引器。它是这样的:

public class NullableDictionnary : Dictionary<string, string> 
{ 
    string null_value; 

    public StringDictionary this[string key] 
    { 
     get 
     { 
      if (key == null) 
      { 
       return null_value; 
      } 
      return base[key]; 
     } 
     set 
     { 
      if (key == null) 
      { 
       null_value = value; 
      } 
      else 
      { 
       base[key] = value; 
      } 
     } 
    } 
} 

希望这可以帮助未来的人。

==========

我把它修改,以这种格式

public class NullableDictionnary : Dictionary<string, object> 
+4

难道你不能通过使对象成为类型参数来继续泛型主题吗? – colithium 2012-01-28 03:44:22

+0

这不起作用。 public StringDictionary this [string key] {... 应该是 public String this [string key] {。另外基地[钥匙]将不会从我的尝试工作。我建议实现IDictionary,并建立全局私有字典对象并处理每种方法的空例。 – 2015-05-15 21:40:13

+4

我想知道你为什么要拼错字典。 – 2015-07-22 17:53:10

7

让我来帮你 “codaddict算法”

'的例子理解字典 in C#'is'Hashmap in Java'in parallel universe。

一些实现是不同的。请参阅下面的示例以更好地理解。

声明的Java的HashMap:

Map<Integer, Integer> pairs = new HashMap<Integer, Integer>(); 

声明C#词典:

Dictionary<int, int> Pairs = new Dictionary<int, int>(); 

从一个位置获取的值:

pairs.put(k - input[i], input[i]); // in Java 
Pairs[k - input[i]] = input[i]; // in C# 

pairs.get(input[i]); // in Java 
Pairs[input[i]];  // in C# 

在位置设置的值

整体示例可以从下面的Codaddict算法中观察到。

codaddict在Java的算法:

import java.util.HashMap; 

public class ArrayPairSum { 

    public static void printSumPairs(int[] input, int k) 
    { 
     Map<Integer, Integer> pairs = new HashMap<Integer, Integer>(); 

     for (int i = 0; i < input.length; i++) 
     { 
      if (pairs.containsKey(input[i])) 
       System.out.println(input[i] + ", " + pairs.get(input[i])); 
      else 
       pairs.put(k - input[i], input[i]); 
     } 

    } 

    public static void main(String[] args) 
    { 
     int[] a = { 2, 45, 7, 3, 5, 1, 8, 9 }; 
     printSumPairs(a, 10); 

    } 
} 

Codaddict在C#算法

using System; 
using System.Collections.Generic; 

class Program 
{ 
    static void checkPairs(int[] input, int k) 
    { 
     Dictionary<int, int> Pairs = new Dictionary<int, int>(); 

     for (int i = 0; i < input.Length; i++) 
     { 
      if (Pairs.ContainsKey(input[i])) 
      { 
       Console.WriteLine(input[i] + ", " + Pairs[input[i]]); 
      } 
      else 
      { 
       Pairs[k - input[i]] = input[i]; 
      } 
     } 
    } 
    static void Main(string[] args) 
    { 
     int[] a = { 2, 45, 7, 3, 5, 1, 8, 9 }; 
     //method : codaddict's algorithm : O(n) 
     checkPairs(a, 10); 
     Console.Read(); 
    } 
} 
0

答案是

字典

采取看看我的功能,其简单的添加使用最重要的成员函数中词典

这个函数返回false,如果列表中包含重复项

public static bool HasDuplicates<T>(IList<T> items) 
    { 
     Dictionary<T, bool> mp = new Dictionary<T, bool>(); 
     for (int i = 0; i < items.Count; i++) 
     { 
      if (mp.ContainsKey(items[i])) 
      { 
       return true; // has duplicates 
      } 
      mp.Add(items[i], true); 
     } 
     return false; // no duplicates 
    } 
0

我只是想给我的两分钱。
这是根据@Powerlord的答案。

放入“null”而不是null字符串。

private static Dictionary<string, string> map = new Dictionary<string, string>(); 

public static void put(string key, string value) 
{ 
    if (value == null) value = "null"; 
    map[key] = value; 
} 

public static string get(string key, string defaultValue) 
{ 
    try 
    { 
     return map[key]; 
    } 
    catch (KeyNotFoundException e) 
    { 
     return defaultValue; 
    } 
} 

public static string get(string key) 
{ 
    return get(key, "null"); 
} 
0

使用字典 - 它使用散列表,但是类型安全。

而且,

int a = map.get(key); 
//continue with your logic 

您的Java代码将最好在C#这样编码:

int a; 
if(dict.TryGetValue(key, out a)){ 
//continue with your logic 
} 

这样,您就可以范围的变量,需要“一”块内,如果您稍后需要,它仍然可以在该块外部访问。