我应该使用什么类型的集合? [英] What type of collection should I use?

查看:82
本文介绍了我应该使用什么类型的集合?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有大约10000条记录。每个记录有两个字段:一个字段是一个字符串长度最多300个字符,另一个字段是一个十进制值。这就好比一个产品目录,产品名称及每个产品的价格。

I have approximately 10,000 records. Each records has 2 fields: one field is a string up to 300 characters in length and the other field is a decimal value. This is like a product catalog with product names and the price of each product.

我需要做的是让用户输入任何内容,显示包含所有产品词与他们在一个列表框的价格在一起。这就是全部。

What I need to do is allow the user to type any word and display all products containing that word together with their prices in a listbox. That's all.


  1. 什么类型的集合是最适合这种情况?

  2. 如果我需要排序的基础上无论是产品的名称或价格,将选择仍是一样的吗?

现在我使用的XML文件,但我想用一个集合,这样我可以嵌入所有的值中的代码更简单。感谢您的建议。

Right now I am using an XML file, but I thought using a collection so that I can embed all the values in the code is simpler. Thanks for your suggestions.

推荐答案

一个字典将做的工作。但是,如果你在做快速部分匹配(例如搜索作为用户类型),你可以通过创建多个按键,指向同一个项目得到更好的性能。例如,单词苹果可以位于与鸭,应用,申请和苹果

A Dictionary will do the job. However, if you are doing rapid partial matches (e.g. search as the user types) you may get better performance by creating multiple keys which point to the same item. For example, the word "Apple" could be located with "Ap", "App", "Appl", and "Apple".

我已经使用这种方法的类似数量的记录有很好的效果。我已经把我的10K的源项目成左右50K唯一的密钥。每个字典条目指向包含到该学期所有匹配的参考名单。然后,您可以更有效地搜索这个更小的列表。尽管众多列出了此创建的,内存占用还是比较合理的。

I have used this approach on a similar number of records with very good results. I have turned my 10K source items into about 50K unique keys. Each of these Dictionary entries points to a list containing references to all matches for that term. You can then search this much smaller list more efficiently. Despite the large number of lists this creates, the memory footprint is quite reasonable.

您也可以弥补自己的钥匙,如果需要,常见的拼写错误或点重定向到相关项目。这也消除了大部分的具有独特的键的问题,因为每一个关键点的列表。单个项目可以由每个在其名称的词进行分类;如果你有多个字词长的产品名称,这是非常有用的。当你的分类项,在名字每个字都可以映射到一个或多个键。

You can also make up your own keys if desired to redirect common misspellings or point to related items. This also eliminates most of the issues with unique keys because each key points to a list. A single item may be classified by each of the words in its name; this is extremely useful if you have long product names with multiple words in it. When classifying your items, each word in the name can be mapped to one or more keys.

我还要指出的是建筑和分类10K项目不应该多久如果做得正确(夫妇几百毫秒是合理的)。结果可以缓存,只要你想用应用缓存,或静态成员。

I should also point out that building and classifying 10K items shouldn't take long if done correctly (couple hundred milliseconds is reasonable). The results can be cached for as long as you want using Application, Cache, or static members.

要总结,由此产生的结构是一个词典<字符串列表< T>> 其中字符串是短(2 -6字符效果很好),但唯一的密钥。每个关键点,一个列表< T> (或其他集合,如果你愿意的话)相匹配的关键哪些项目。当执行搜索时,找到其中由用户提供的术语相匹配的关键。根据您的密钥的长度,你可能会截断用户的搜索到你的最大密钥长度。找到正确的子集后,然后搜索该集合使用您所希望的任何方法完全或部分匹配。

To summarize, the resulting structure is a Dictionary<string, List<T>> where the string is a short (2-6 characters works well) but unique key. Each key points to a List<T> (or other collection, if you are so inclined) of items which match that key. When a search is performed, you locate the key which matches the term provided by the user. Depending on the length of your keys, you may truncate the user's search to your maximum key length. After locating the correct child collection, you then search that collection for a complete or partial match using whatever methodology you wish.

最后,您可能希望创建一个轻量级结构列表中的每个项目,以便可以存储有关该项目的其他信息。例如,您可以创建存储的名称,价格,部门和产品的普及一个小产品类。这可以帮助你完善你展示给用户的结果。

Lastly, you may wish to create a lightweight structure for each item in the list so that you can store additional information about the item. For example, you might create a small Product class which stores the name, price, department, and popularity of the product. This can help you refine the results you show to the user.

ALL-IN-一切,你可以实时进行智能的,详细的,模糊搜索。

All-in-all, you can perform intelligent, detailed, fuzzy searches in real-time.

上述结构应当提供功能大致相当于一个线索

The aforementioned structures should provide functionality roughly equivalent to a trie.

这篇关于我应该使用什么类型的集合?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆