.net 是否有允许重复的字典/排序列表的替代方法?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/551901/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 12:08:51  来源:igfitidea点击:

Is there an alternative to Dictionary/SortedList that allows duplicates?

.netdictionarypriority-queuemultimap

提问by Sedat Kapanoglu

Possible Duplicate:
C# Sortable collection which allows duplicate keys

可能的重复:
允许重复键的 C# Sortable 集合

Basically I'd like to make a Dictionary work with duplicate keys without going into custom comparer implementations. There is an idea of:

基本上,我想让字典使用重复的键,而无需进入自定义比较器实现。有一个想法:

  Dictionary<key, List<value>>

but it still has some overhead. I wish Dictionary had "AllowDuplicates".

但它仍然有一些开销。我希望字典有“AllowDuplicates”。

采纳答案by LukeH

If you're using .NET 3.5 then Lookupis probably what you're after.

如果您使用的是 .NET 3.5,那么Lookup可能就是您所追求的。

回答by Mitch Wheat

.NET 2.0: PowerCollectionscontains the OrderedMultiDictionary.

.NET 2.0:PowerCollections包含OrderedMultiDictionary.

回答by thangcao

You still can use SortedList and try to make a unique key by combining your value and a Guid into a class. In this case, you must implement the IComparer<NewKey>for your new key, something like:

您仍然可以使用 SortedList 并尝试通过将您的值和 Guid 组合到一个类中来创建一个唯一的键。在这种情况下,您必须IComparer<NewKey>为新密钥实现 ,例如:

class MyKey
{
    public Guid Guid { get; set; }
    public float Value { get; set; }
}

class MyComparer : IComparer<MyKey>
{

    public int Compare(MyKey x, MyKey y)
    {
        if (x == null || y == null)
            throw new InvalidOperationException("both of parameters must be not null");
        if (x.Value < y.Value) return -1;
        if (x.Value > y.Value) return 1;
        return 0;
    }
}

and then

进而

var mySortedList = new SortedList<MyKey, MyValue>(new MyComparer());

回答by B08AH

That does not work. As soon as you return 0 from the comparer, it will throw "duplicate" exception.

那行不通。一旦您从比较器返回 0,它就会抛出“重复”异常。

You don't need classes encapsulation or anything, just make a comparer that does not return 0 (equal) result. Here is an example for inttype of key

您不需要类封装或任何东西,只需制作一个不返回 0(相等)结果的比较器。这是int密钥类型的示例

class MyComparer : IComparer<int>
{

  public int Compare(int x, int y)
  {
    if (x < y)
      return -1;
    else return 1;
  }
}

回答by Ryan Emerle

Not in the Fx < 3.5.. You can implement one, obviously, with a Dictionary of IList objects. But then you have the encapsulation issue/responsibility.

不在 Fx < 3.5 中。显然,您可以使用 IList 对象字典来实现。但是你有封装问题/责任。

If you're using .NET 3.5, use the Lookupclass.

如果您使用 .NET 3.5,请使用Lookup类。

回答by Rajasekhar

I came across with same issue.. I needed a sortedList which can allow Duplicate Keys..

我遇到了同样的问题..我需要一个可以允许重复键的 sortedList..

var sortList = new SortedList<string, IDictionary<string, object>>();

but this didnt work.. so i used

但这没有用..所以我用

var list = new List<KeyValuePair<string, IDictionary<string, object>>>();

add new data to it as ..

将新数据添加到它作为 ..

list.Add(new KeyValuePair<string, IDictionary<string, object>>>(value, Dictionary));

with linq i sorted it with no problem..

使用 linq 我对它进行了排序,没有问题..

Try List<KeyValuePair<TKey, List<TValue>>>();

尝试 List<KeyValuePair<TKey, List<TValue>>>();

回答by Dave Swersky

By definition, a Dictionary contains unique keys. Your example above is effectively a sort of two-dimensional keyed array, a structure I've used many times. Why would you want to have duplicate keys? If you did, how would the Dictionary uniquely address its members?

根据定义,字典包含唯一键。您上面的示例实际上是一种二维键控数组,这是我多次使用的结构。为什么要使用重复的键?如果你这样做了,字典将如何唯一地解决它的成员?