C# 获取字典中的最后一个元素?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1018168/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Get the last element in a dictionary?
提问by subprime
My dictionary:
我的字典:
Dictionary<double, string> dic = new Dictionary<double, string>();
How can I return the last element in my dictionary?
如何返回字典中的最后一个元素?
采纳答案by JaredPar
What do you mean by Last? Do you mean Last value added?
最后是什么意思?你的意思是最后的增值?
The Dictionary<TKey,TValue>
class is an unordered collection. Adding and removing items can change what is considered to be the first and last element. Hence there is no way to get the Last element added.
该Dictionary<TKey,TValue>
班是一个无序的集合。添加和删除项目可以更改被视为第一个和最后一个元素的内容。因此无法添加 Last 元素。
There is an ordered dictionary class available in the form of SortedDictionary<TKey,TValue>
. But this will be ordered based on comparison of the keys and not the order in which values were added.
有一个以SortedDictionary<TKey,TValue>
. 但这将根据键的比较而不是添加值的顺序进行排序。
EDIT
编辑
Several people have mentioned using the following LINQ style approach
有几个人提到使用以下 LINQ 风格的方法
var last = dictionary.Values.Last();
Be very wary about using this method. It will return the last value in the Values collection. This may or may not be the last value you added to the Dictionary. It's probably as likely to not be as it is to be.
使用这种方法时要非常小心。它将返回 Values 集合中的最后一个值。这可能是也可能不是您添加到字典中的最后一个值。可能不会像现在这样。
回答by Lance Harper
If you just want the value, this should work (assuming you can use LINQ):
如果您只想要该值,这应该可以工作(假设您可以使用 LINQ):
dic.Values.Last()
回答by Jake Pearson
You could use:
你可以使用:
dic.Last()
But a dictionary doesn't really have a last element (the pairs inside aren't ordered in any particular way). The last item will always be the same, but it's not obvious which element it might be.
但是字典并没有真正的最后一个元素(里面的对没有以任何特定的方式排序)。最后一项将始终相同,但它可能是哪个元素并不明显。
回答by Meta-Knight
With .Net 3.5:
使用 .Net 3.5:
string lastItem = dic.Values.Last()
string lastKey = dic.Keys.Last()
...but keep in mind that a dictionary is not ordered, so you can't count on the fact that the values will remain in the same order.
...但请记住,字典没有排序,因此您不能指望值将保持相同的顺序。
回答by Chris Doggett
If you're using .NET 3.5, look at:
如果您使用的是 .NET 3.5,请查看:
dic.Keys.Last()
If you want a predictable order, though, use:
但是,如果您想要一个可预测的顺序,请使用:
IDictionary<int, string> dic = new SortedDictionary<int, string>();
回答by LBushkin
Dictionaries are unordered collections - as such, there is no concept of a first or last element. If you are looking for a class that behaves like a dictionary but maintains the insertion order of items, consider using OrderedDictionary
.
字典是无序的集合——因此,没有第一个或最后一个元素的概念。如果您正在寻找一个行为类似于字典但维护项目插入顺序的类,请考虑使用OrderedDictionary
.
If you are looking for a collection that sorts the items, consider using SortedDictionary<TKey,TValue>
.
如果您正在寻找对项目进行排序的集合,请考虑使用SortedDictionary<TKey,TValue>
.
If you have an existing dictionary, and you are looking for the 'last' element given some sort order, you could use linq to sort the collection, something like:
如果您有一个现有的字典,并且您正在寻找给定某种排序顺序的“最后一个”元素,则可以使用 linq 对集合进行排序,例如:
myDictionary.Values.OrderBy( x => x.Key ).Last();
By wary of using Dictionary.Keys.Last()
- while the key list is sorted using the default IComparer
for the type of the key, the value you get may not be the value you expect.
谨慎使用Dictionary.Keys.Last()
- 虽然键列表使用IComparer
键类型的默认值进行排序,但您获得的值可能不是您期望的值。
回答by RichardOD
Consider creating a custom collection that contains a reference in the Add
method of the custom collection. This would set a private field containing the last added key/value(or both) depending on your requirements.
考虑在自定义集合的Add
方法中创建一个包含引用的自定义集合。这将根据您的要求设置一个包含最后添加的键/值(或两者)的私有字段。
Then have a Last()
method that returns this. Here's a proof of concept class to show what I mean (please don't knock the lack of interface implementation etc- it is sample code):
然后有一个Last()
返回 this的方法。这是一个概念证明类来说明我的意思(请不要敲缺少接口实现等 - 它是示例代码):
public class LastDictionary<TKey, TValue>
{
private Dictionary<TKey, TValue> dict;
public LastDictionary()
{
dict = new Dictionary<TKey, TValue>();
}
public void Add(TKey key, TValue value)
{
LastKey = key;
LastValue = value;
dict.Add(key, value);
}
public TKey LastKey
{
get; private set;
}
public TValue LastValue
{
get; private set;
}
}
回答by bruno conde
From the docs:
从文档:
For purposes of enumeration, each item in the dictionary is treated as a KeyValuePair structure representing a value and its key. The order in which the items are returned is undefined.
出于枚举的目的,字典中的每个项目都被视为表示值及其键的 KeyValuePair 结构。项目返回的顺序是 undefined。
So, I don't think you can rely on Dictionary
to return the last element.
所以,我认为你不能依靠Dictionary
返回最后一个元素。
Use another collection. Maybe SortedDictionary
...
使用另一个集合。也许SortedDictionary
...
回答by Hardwareguy
A dictionary isn't meant to be accessed in order, so first, last have no meaning. Do you want the value indexed by the highest key?
字典不是按顺序访问的,所以 first, last 没有意义。你想要由最高键索引的值吗?
Dictionary<double, string> dic = new Dictionary<double, string>();
double highest = double.MinValue;
string result = null;
foreach(double d in dic.keys)
{
if(d > highest)
{
result = dic[d];
highest = d;
}
}
回答by Erick
Instead of using:
而不是使用:
Dictionary<double, string>
...you could use:
...你可以使用:
List<KeyValuePair<double, string>>
This would allow you to use the indexer to access the element by order instead of by key.
这将允许您使用索引器按顺序而不是按键访问元素。