简体   繁体   English

如何创建自己的高效数据结构?

[英]How to create my own efficient data structure?

I'm making a program in which I need to save objects that are not repeated and must be sorted into a data structure. 我正在制作一个程序,我需要保存不重复的对象,并且必须将其分类到数据结构中。 Access, modification and removal of the object have to be very efficient. 访问,修改和删除对象必须非常有效。

First I thought about making a Map < String,Object > where the key is the name (attribute) of the object. 首先,我考虑制作一个Map <String,Object>,其中键是对象的名称(属性)。 But my teacher told me it was inefficient because I was duplicating content and I should have to create my own structure without using lists, vectors, .. or at least not directly, that is, I have to override the implementation. 但我的老师告诉我这是低效的,因为我复制内容,我应该创建自己的结构而不使用列表,向量,或者至少不是直接,也就是说,我必须覆盖实现。

But I do not know where to start and what is a good choice. 但我不知道从哪里开始,什么是一个好的选择。 What advice would you give me to access the object through its name in an efficient way? 您有什么建议让我以有效的方式通过其名称访问对象?

Thanks! 谢谢!

You didn't mention how efficient it needs to be so I assume a TreeSet is what you need 你没有提到它需要多高效,所以我假设你需要一个TreeSet

Operations you mentioned are done in log(n) 你提到的操作是在log(n)中完成的

从扩展Set开始(因为你不想重复任何对象)

If i understand correctly, you want to map unique string names of objects to object references, and you want to be able to efficiently (faster than O(n)). 如果我理解正确,您希望将对象的唯一字符串名称映射到对象引用,并且您希望能够有效(比O(n)更快)。

A sorted array allows O(log n) insertion, search, and linear-time deletion. 排序数组允许O(log n)插入,搜索和线性时间删除。 A balanced BST allows O(log n) insertion, search, and deletion. 平衡的BST允许O(log n)插入,搜索和删除。 A simple dictionary ( HashTable ) allows O(1) amortized insertion, search, and deletion, but the dictionary contents cannot be efficiently enumerated (Java allows dictionary contents to be enumerated in somewhat meaningless hash order). 简单字典( HashTable )允许O(1)分期插入,搜索和删除,但字典内容无法有效枚举(Java允许字典内容以一些无意义的哈希顺序进行枚举)。 If you need to enumerate the objects in sorted order, a more efficient approach would be to maintain a sorted linked list of objects (by name) and use a dictionary to map names to liked list entries for O(1) amortized lookup. 如果需要按排序顺序枚举对象,则更有效的方法是维护已排序的对象链接列表(按名称),并使用字典将名称映射到O(1)分摊查找的首选列表条目。

As previously answered, TreeSet is a solid starting point using the Java library. 如前所述, TreeSet是使用Java库的坚实起点。

You may want a LinkedHashMap. 您可能需要LinkedHashMap。 This is an order-preserving hash map which can be iterated over. 这是一个可以迭代的保持顺序的哈希映射。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM