【发布时间】:2009-10-23 12:04:32
【问题描述】:
我看到this question。
如何在 .Net 3.5 中获取 SortedDictionary 中的最后一个元素。
【问题讨论】:
标签: c# collections sorteddictionary
我看到this question。
如何在 .Net 3.5 中获取 SortedDictionary 中的最后一个元素。
【问题讨论】:
标签: c# collections sorteddictionary
您可以使用 LINQ:
var lastItem = sortedDict.Values.Last();
你也可以得到最后一个key:
var lastkey = sortedDict.Keys.Last();
你甚至可以得到最后一个键值对:
var lastKeyValuePair = sortedDict.Last();
这将为您提供具有Key 和Value 属性的KeyValuePair<TKey, TValue>。
请注意,如果字典为空,这将引发异常;如果您不想这样,请致电LastOrDefault。
【讨论】:
Last 扩展方法会给你结果,但它必须枚举整个集合才能到达那里。真可惜SortedDictionary<K, V> 没有公开Min 和Max 成员,特别是考虑到在内部它由具有Min 和Max 属性的SortedSet<KeyValuePair<K, V>> 支持。
如果 O(n) 不可取,您有几个选择:
切换到SortedList<K, V>。再次出于某种原因,BCL 默认情况下不会打包。您可以使用索引器在 O(1) 时间内获取最大值(或最小值)。使用扩展方法进行扩展会很好。
//Ensure you dont call Min Linq extension method.
public KeyValuePair<K, V> Min<K, V>(this SortedList<K, V> dict)
{
return new KeyValuePair<K, V>(dict.Keys[0], dict.Values[0]); //is O(1)
}
//Ensure you dont call Max Linq extension method.
public KeyValuePair<K, V> Max<K, V>(this SortedList<K, V> dict)
{
var index = dict.Count - 1; //O(1) again
return new KeyValuePair<K, V>(dict.Keys[index], dict.Values[index]);
}
SortedList<K, V> 带有其他惩罚。所以你可能想看看:What's the difference between SortedList and SortedDictionary?
编写你自己的SortedDictionary<K, V> 类。这是非常微不足道的。将SortedSet<KeyValuePair<K, V>> 作为内部容器,并根据Key 部分进行比较。比如:
public class SortedDictionary<K, V> : IDictionary<K, V>
{
SortedSet<KeyValuePair<K, V>> set; //initialize with appropriate comparer
public KeyValuePair<K, V> Min { get { return set.Min; } } //O(log n)
public KeyValuePair<K, V> Max { get { return set.Max; } } //O(log n)
}
这是 O(log n)。没有记录,但我检查了代码。
使用精巧的反射来访问作为 SortedDictionary<K, V> 类的私有成员的支持集并调用 Min 和 Max 属性。可以依靠表达式来编译委托并将其缓存以提高性能。这样做是一个非常糟糕的选择。不敢相信我提出了这个建议。
依赖其他实现,例如。对于TreeDictionary<K, V> from C5。他们有FindMin 和FindMax both of which are O(log n)
【讨论】:
TryGetValue?
您可以使用SortedDictionary.Values.Last();
或者如果你想要键和值
SortedDictionary.Last();
【讨论】:
SortedList 列表...
list[ Keys[Keys.Count - 1] ]; // returns the last entry in list
【讨论】:
正如人们已经指出 Last 扩展将枚举整个集合,它对性能的影响可能是致命的。 仅从 SortedDict 中删除 10000 个最后的元素,就比对 SortedSet 的类似操作花费了更多的时间。
SortedSet 删除经过的毫秒数:8
SortedDict 删除经过的毫秒数:3697
// 在下面的代码中,ss 是 SortedSet,sd 是 SortedDictionary,都包含相同的 10000 个元素。
sw.Start();
while (ss.Count != 0)
{
ss.Remove(ss.Max);
}
sw.Stop();
Console.WriteLine("SortedSet Removal Elapsed ms : {0}", sw.ElapsedMilliseconds);
sw.Reset();
sw.Start();
while (sd.Count != 0)
{
sd.Remove(sd.Keys.Last());
}
sw.Stop();
Console.WriteLine("Dict Removal Elapsed ms : {0}", sw.ElapsedMilliseconds);
【讨论】: