從Java世界進入C#之一是否存在一個HashMap等價物?如果不是,你會推薦什麼?C#Java HashMap等價物
回答
Dictionary
可能是最接近的。 System.Collections.Generic.Dictionary
實現了System.Collections.Generic.IDictionary
接口(它類似於Java的Map
接口)。
一些顯着的差別,你應該知道的:
- 添加/獲取項目
- Java的HashMap具有
put
和get
方法設置/獲取項目myMap.put(key, value)
MyObject value = myMap.get(key)
- C#的字典使用
[]
索引用於設置/獲取項目myDictionary[key] = value
MyObject value = myDictionary[key]
- Java的HashMap具有
null
鍵- Java的
HashMap
允許空關鍵小號 - .NET的
Dictionary
如果您嘗試添加一個空鍵
- Java的
- 添加重複鍵
- Java的
HashMap
將替換爲新一個現有的值拋出ArgumentNullException
。 - 如果您使用
[]
索引,.NET的Dictionary
將用新值替換現有值。如果您使用Add
方法,則會拋出ArgumentException
。
- Java的
- 試圖獲取一個不存在的關鍵
- Java的
HashMap
將返回null。 - .NET的
Dictionary
將拋出一個KeyNotFoundException
。您可以使用TryGetValue
方法,而不是[]
索引,以避免這種情況:
MyObject value = null; if (!myDictionary.TryGetValue(key, value)) { /* key doesn't exist */ }
- Java的
Dictionary
的有ContainsKey
方法,可以幫助解決前兩個問題。
從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>
難道你不能通過使對象成爲類型參數來繼續泛型主題嗎? – colithium 2012-01-28 03:44:22
這不起作用。 public StringDictionary this [string key] {... 應該是 public String this [string key] {。另外基地[鑰匙]將不會從我的嘗試工作。我建議實現IDictionary,並建立全局私有字典對象並處理每種方法的空例。 – 2015-05-15 21:40:13
我想知道你爲什麼要拼錯字典。 – 2015-07-22 17:53:10
讓我來幫你 「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();
}
}
答案是
字典
採取看看我的功能,其簡單的添加使用最重要的成員函數中詞典
這個函數返回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
}
我只是想給我的兩分錢。
這是根據@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");
}
使用字典 - 它使用散列表,但是類型安全。
而且,
int a = map.get(key);
//continue with your logic
您的Java代碼將最好在C#這樣編碼:
int a;
if(dict.TryGetValue(key, out a)){
//continue with your logic
}
這樣,您就可以範圍的變量,需要「一」塊內,如果您稍後需要,它仍然可以在該塊外部訪問。
- 1. VB.NET HashMap等價物
- 2. BufferedImage.getRGB C#等價物?
- 3. Java中C#鎖定的等價物?
- 4. MATLAB的robustfit的Java或C等價物
- 5. 來自Obj的Java等價物C
- 6. 尋找C#的XmlConvert.EncodeName的Java等價物
- 7. Java的等價物C#大理性
- 8. Java中的C#OnDeserialized/OnSerializaed的等價物?
- 9. Java數據結構/ C++ STL等價物?
- 10. C++ Java靜態最終等價物
- 11. C#中的Java Observer/Observable等價物?
- 12. C#Array.GetLength的Java等價物(i)
- 13. 什麼是Java DecimalFormat的c#等價物?
- 14. 什麼是C++ deque的Java等價物?
- 15. 什麼是Java Socket.getInetAddress()的C#等價物?
- 16. Java的Ruby等價物ObjectSpace.each_object
- 17. Java的Drupal等價物?
- 18. zedgraph或java的等價物
- 19. Java的等價物GetPositionFromCharIndex
- 20. C#中func_get_arg的等價物?
- 21. Encoding.Unicode.GetByteCount(str)的C++等價物?
- 22. C#中fmodf的等價物?
- 23. Python等價物repr()?
- 24. C/C++中的CreateObject等價物? (COM Interop)
- 25. C++ BITMAPINFOHEADER biBitCount在C#中的等價物
- 26. C++/C#數組移位等價物
- 27. C#ManualResetEvent的Objective-C/Cocoa等價物
- 28. BeanShell的等價物淨
- 29. xtend的sprintf等價物
- 30. 是否有.Net System.Data的Java等價物?
沒有一個確切的等價物(在JAVA中,HashMap允許空值和null鍵)http://download.oracle.com/javase/1.4.2/docs/api/java/util/HashMap.html – 2010-12-25 18:55:56
是的,詞典很接近但不完全確切。 – Powerlord 2010-12-25 21:00:37
注意,'Dictionary'在添加重複鍵時拋出異常。 – 2011-12-27 20:08:46