最好查找数据结构来存储唯一密钥(词典不值) [英] Best lookup data structure to store only the keys (Dictionary without value)
本文介绍了最好查找数据结构来存储唯一密钥(词典不值)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
.Net中具有高性能查找的最佳数据结构,如二叉树实现,但只存储键(字符串键)?
What is the best data structure in .Net with high performance lookup, like a binary tree implementation, but to store only the keys (string keys) ?
仅需要检查集合中是否存在特定键。喜欢:
We need only to check if a certain key is present in the collection. Like:
Dictonary<string, object> myKeys;
myKeys.Add("key1", null);
myKeys.Add("key2", null);
// Dozens or hundreds keys
Assert.IsTrue(myKeys.Contains("key1"));
推荐答案
A System.Collections.Generic ):
HashSet是包含唯一元素的无序集合。它有
标准收集操作添加,删除,包含,但由于
使用基于散列的实现,这些操作是O(1)。
HashSet is an unordered collection containing unique elements. It has the standard collection operations Add, Remove, Contains, but since it uses a hash-based implementation, these operation are O(1).
例如
HashSet<int> evenNumbers = new HashSet<int>();
HashSet<int> oddNumbers = new HashSet<int>();
for (int i = 0; i < 5; i++)
{
// Populate numbers with just even numbers.
evenNumbers.Add(i * 2);
// Populate oddNumbers with just odd numbers.
oddNumbers.Add((i * 2) + 1);
}
if (evenNumbers.Contains(2))
{
Console.WriteLine("2 is even.");
}
- 介绍HashSet
- HashSet与列表效果
- Introducing HashSet
- HashSet vs. List performance
这篇关于最好查找数据结构来存储唯一密钥(词典不值)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文