从Java转到C#,是否有HashMap的等效物?如果没有,你会推荐什么?
Dictionary
可能是最接近的。 System.Collections.Generic.Dictionary
实现了 System.Collections.Generic.IDictionary
接口(类似于 Java 的 Map
接口)。
你应该注意到一些显著的区别:
put
和get
方法,用于设置/获取元素
myMap.put(key, value)
MyObject value = myMap.get(key)
[]
索引来设置/获取元素
myDictionary[key] = value
MyObject value = myDictionary[key]
null
键
HashMap
允许使用null键Dictionary
如果尝试添加null键,则会抛出ArgumentNullException
HashMap
将使用新值替换现有值。Dictionary
如果使用[]
索引,则将使用新值替换现有值。 如果使用Add
方法,则会抛出ArgumentException
。HashMap
将返回null。Dictionary
将抛出KeyNotFoundException
。 您可以使用TryGetValue
方法而不是[]
索引来避免这种情况:MyObject value = null;
if (!myDictionary.TryGetValue(key, out value)) { /* key doesn't exist */ }
Dictionary
有一个ContainsKey
方法,可以帮助处理前两个问题。
我需要一个接受"null"键的字典,但似乎没有内置的方法,所以我自己编写了一个。实际上很简单。我继承了Dictionary,添加了一个私有字段来保存"null"键的值,然后重写了索引器。代码如下:
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>
让我用“codaddict算法”的例子来帮助你理解它。
C#中的'Dictionary'在平行宇宙中是Java中的'Hashmap'。
某些实现是不同的。请参阅下面的示例以更好地理解。
声明Java HashMap:
Map<Integer, Integer> pairs = new HashMap<Integer, Integer>();
声明 C# 字典:
Dictionary<int, int> Pairs = new Dictionary<int, int>();
从一个位置获取值:
pairs.get(input[i]); // in Java
Pairs[input[i]]; // in C#
设置某个位置的值:
pairs.put(k - input[i], input[i]); // in Java
Pairs[k - input[i]] = input[i]; // in C#
以下是Codaddict算法的总体示例。
Java版的codaddict算法:
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();
}
}
new()
,而不是像之前需要输入 new Dictionary<int, int>()
。 :)(尽管这个答案发布时 C#9.0 还未推出,但建议使用本方法编写更易读的代码) - dimitar.bogdanov使用字典 - 它使用哈希表,但是类型安全。
此外,您的Java代码为
int a = map.get(key);
//continue with your logic
int a;
if(dict.TryGetValue(key, out a)){
//continue with your logic
}
通过这种方式,您可以在块内限定变量"a"的作用范围,如果以后需要,它仍然可以在块外部访问。
dict.TryGetValue(key, out int a)
方法。 - nevvermindDictionary<TKey, TValue>
更好,因为它在编译时进行类型检查,并且不需要对值类型进行装箱。 - Thorarin我只是想提供我的意见。
这是根据@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");
}
答案是
字典
看一下我的函数,它很简单,使用了字典中最重要的成员函数add。
如果列表包含重复项,则此函数返回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
}
Dictionary
会抛出异常。 - Rubens MariuzzoTryGetValue
返回 false,则它还会将value
设置为其类型的默认值(通常为0或null,类似于HashMap
返回的内容)。因此,如果您对此感到满意,在某些情况下甚至不需要检查TryGetValue
的布尔结果。 - Wizou