对于包含 10 个或更少项目的集合,我应该使用字典吗?还是有更好的选择?

发布于 2024-08-24 05:49:33 字数 128 浏览 6 评论 0原文

我有一个对象列表,我需要尽快找到一个对象(通过它的名称属性)。我应该使用什么数据结构?我知道我可以使用字典,但列表中的项目永远不会超过 10 个,如果我没记错的话,如果集合包含 10 个或更少的项目,字典将实现为数组。

谢谢。

I have a list of objects and I need to find an object as quickly as possible (by it's name property). What data-structure should I use? I know I can use a Dictionary, but there wont ever be more than 10 items in the list, and if I remember correctly the dictionary is implemented as an array if the collection contains 10 items or less.

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

提赋 2024-08-31 05:49:33

MSDN 建议对包含 10 个项目的集合使用 ListDictionary或更少:

这是使用单链表的 IDictionary 的简单实现。如果元素数量为 10 或更少,则它比 Hashtable 更小且更快。如果性能对于大量元素很重要,则不应使用此方法。

MSDN recommends the ListDictionary for collections with 10 items or less:

This is a simple implementation of IDictionary using a singly linked list. It is smaller and faster than a Hashtable if the number of elements is 10 or less. This should not be used if performance is important for large numbers of elements.

谜兔 2024-08-31 05:49:33

如果您确定项目少于 10 个,您可能需要考虑 System.Collections.Specialized.ListDictionary。

还要考虑 System.Collections.Specialized.HybridDictionary ,它会在大小增加到阈值以上时切换行为(开销很小),如果您的假设错误,这会很方便。

You may want to consider the System.Collections.Specialized.ListDictionary if you are certain there will be less than ten items.

Also consider the System.Collections.Specialized.HybridDictionary which switches behaviour (with a small overhead) should the size increase above a threshold, handy if your assumption is wrong.

涙—继续流 2024-08-31 05:49:33

由于您希望通过属性进行尽可能最快的查找,因此您应该使用Dictionary。如果您想要快速查找,大小不会对您造成伤害。这并不是说只有 10 项或更少的 Dictionary 就占用了大量内存。 Dictionary 有一个构造函数,它采用 int 来设置容量。

Since you want fastest possible lookup by a property you should use Dictionary<Key, Value>. The size doesn't hurt you if you want fast lookup. It's not that a Dictionary<Key, Value> of just 10 items or less is taking up a ton of memory. Dictionary<Key, Value> has a constructor that takes an int to set the capacity.

屋檐 2024-08-31 05:49:33

为什么不直接使用哈希表呢?它位于 System.Collections 命名空间中。

Why not just use a Hashtable? It's in the System.Collections namespace.

善良天后 2024-08-31 05:49:33

对于您的情况, KeyedCollection 就可以了。它是一个混合结构,构造函数采用一个参数来表示结构应从线性列表切换到基于哈希的查找。是的,它是通用的。

一个缺点是它是一个抽象类,因此您必须自己派生一个。

public class KeyedCollection<TKey, TItem> : System.Collections.ObjectModel.KeyedCollection<TKey, TItem>
{
    private readonly Func<TItem, TKey> m_keySelector;

    public KeyedCollection(Func<TItem, TKey> keySelector)
        : this(keySelector, null)
    {
    }

    public KeyedCollection(Func<TItem, TKey> keySelector, IEqualityComparer<TKey> comparer)
        : this(keySelector, comparer, 10)
    {
    }

    public KeyedCollection(Func<TItem, TKey> keySelector, IEqualityComparer<TKey> comparer, int dictionaryCreationThreshold)
        : base(comparer, dictionaryCreationThreshold)
    {
        m_keySelector = keySelector;
    }

    protected override TKey GetKeyForItem(TItem item)
    {
        return m_keySelector(item);
    }
}

那么你可以这样称呼它:

var foo = new KeyedCollection<Foo, string>(f => f.Name, null, 10);

For your case, KeyedCollection<TKey, TItem> will do. It is a hybrid structure, the constructor takes a parameter to denote from what size onward the structure should switch from linear list to a hash based lookup. And yes it is generic.

One downside is it is an abstract class, so you will have to derive one yourself.

public class KeyedCollection<TKey, TItem> : System.Collections.ObjectModel.KeyedCollection<TKey, TItem>
{
    private readonly Func<TItem, TKey> m_keySelector;

    public KeyedCollection(Func<TItem, TKey> keySelector)
        : this(keySelector, null)
    {
    }

    public KeyedCollection(Func<TItem, TKey> keySelector, IEqualityComparer<TKey> comparer)
        : this(keySelector, comparer, 10)
    {
    }

    public KeyedCollection(Func<TItem, TKey> keySelector, IEqualityComparer<TKey> comparer, int dictionaryCreationThreshold)
        : base(comparer, dictionaryCreationThreshold)
    {
        m_keySelector = keySelector;
    }

    protected override TKey GetKeyForItem(TItem item)
    {
        return m_keySelector(item);
    }
}

Then you could call it:

var foo = new KeyedCollection<Foo, string>(f => f.Name, null, 10);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文