2009-08-13 60 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 
} 

這樣,您就可以範圍的變量,需要「一」塊內,如果您稍後需要,它仍然可以在該塊外部訪問。