从Java世界到c#世界,有没有HashMap的对等物?如果没有,你推荐什么?
当前回答
请查看MSDN上关于Hashtable类的文档。
表示基于键的哈希代码组织的键和值对的集合。
另外,请记住,这不是线程安全的。
其他回答
字典可能是最接近的。System.Collections.Generic.Dictionary实现了System.Collections.Generic.IDictionary接口(类似于Java的Map接口)。
你应该注意的一些显著差异:
Adding/Getting items Java's HashMap has the put and get methods for setting/getting items myMap.put(key, value) MyObject value = myMap.get(key) C#'s Dictionary uses [] indexing for setting/getting items myDictionary[key] = value MyObject value = myDictionary[key] null keys Java's HashMap allows null keys .NET's Dictionary throws an ArgumentNullException if you try to add a null key Adding a duplicate key Java's HashMap will replace the existing value with the new one. .NET's Dictionary will replace the existing value with the new one if you use [] indexing. If you use the Add method, it will instead throw an ArgumentException. Attempting to get a non-existent key Java's HashMap will return null. .NET's Dictionary will throw a KeyNotFoundException. You can use the TryGetValue method instead of the [] indexing to avoid this: MyObject value = null; if (!myDictionary.TryGetValue(key, out value)) { /* key doesn't exist */ }
Dictionary's有一个ContainsKey方法可以帮助处理前两个问题。
请查看MSDN上关于Hashtable类的文档。
表示基于键的哈希代码组织的键和值对的集合。
另外,请记住,这不是线程安全的。
从c#等价于Java HashMap
我需要一个接受“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's algorithm"的例子来帮助你们理解
“c#中的字典”在平行宇宙中是“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();
}
}
使用字典-它使用哈希表,但类型安全。
的Java代码
int a = map.get(key);
//continue with your logic
最好是这样用c#编写的:
int a;
if(dict.TryGetValue(key, out a)){
//continue with your logic
}
通过这种方式,您可以在块内确定变量“a”的需求,并且如果稍后需要它,它仍然可以在块外访问。
推荐文章
- 替换c#字符串中的多个字符
- 将枚举转换为集合/列表
- 在Java中创建对象数组
- Java中字符串的字节数
- 将XML字符串转换为对象
- ToList()——它是否创建一个新列表?
- IntelliJ IDEA with Junit 4.7”!!JUnit 3.8或更高版本:
- 既然JavaScript和Java没有任何关系,它为什么被称为JavaScript ?
- 番石榴vs Apache Commons
- Java是编译型编程语言还是解释型编程语言?
- 连接网络共享时,如何提供用户名和密码
- ProcessStartInfo挂在“WaitForExit”?为什么?
- 我如何通过Java应用程序使用GMail,雅虎或Hotmail发送电子邮件?
- 注释@GetMapping和@RequestMapping(method = RequestMethod.GET)之间的区别
- lambda表达式中使用的变量应该是final或有效final