Java:集合和“数据结构”之间的区别
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14421121/
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
Java: Difference Between a collection and 'Data Structure'
提问by user1888243
In Java, I don't understand a collection vs a 'data structure'. It seems to me that collection refers to list, set, map, queue, and 'data structure' refers to the data structure used to implement the collection such as an array, linked list, or tree. For example ArrayList and LinkedList are both collection, but their data structure are respectively an array, and a linked list. Am I correct, or am I confusing terms?
在 Java 中,我不理解集合与“数据结构”。在我看来,集合是指列表、集合、映射、队列,而“数据结构”是指用于实现集合的数据结构,例如数组、链表或树。例如 ArrayList 和 LinkedList 都是集合,但它们的数据结构分别是数组和链表。我是正确的,还是我混淆了术语?
回答by Jeff Storey
A data structure is a generic term for an object that represents some sort of data, so a linked list, array, etc are all data structures. A collection in the Java sense refers to any class that implements the Collection
interface. A collection in a generic sense is just a group of objects.
数据结构是表示某种数据的对象的通用术语,因此链表、数组等都是数据结构。Java 意义上的集合是指实现该Collection
接口的任何类。一般意义上的集合只是一组对象。
回答by Siddharth
A data structure is how the data is represented inside the storage in memory. A collection is how it can be accessed. I stress on the word "can".
数据结构是数据在内存中的存储中的表示方式。集合是可以访问它的方式。我强调“可以”这个词。
If you store data in a LinkedList and sort it, the performance will drop. The same algorithm if you use a ArrayList the performance will enhance. Just by changing the way its represented in memory will help various factors.
如果将数据存储在 LinkedList 中并对其进行排序,则性能会下降。如果您使用 ArrayList 相同的算法,则性能将提高。仅仅通过改变它在内存中的表示方式将有助于各种因素。
You "can" access it using a collection representation, you "can" also use the "index" to access the data. You "can" also go getFirst, getNext, getPrev.
您“可以”使用集合表示访问它,您也“可以”使用“索引”来访问数据。您“也可以”转到 getFirst、getNext、getPrev。
Your confusion is between internal storage and accessing the storage. Separate the 2.
您的困惑在于内部存储和访问存储之间。分开2。
回答by Rob
A data structure has the notion of some kind of schema, e.g. a representation of a house would list things like square footage, bedrooms, etc. That's what's usually meant there: how is the structure of the domain represented as data?
数据结构具有某种模式的概念,例如,房屋的表示会列出平方英尺、卧室等内容。这就是那里通常的意思:域的结构如何表示为数据?
A collection is, as Jeff says, just a set of objects. Collections do have structure, but their structure is solely organizational, e.g. a Tree, or a List or a LinkedList.
正如杰夫所说,集合只是一组对象。集合确实有结构,但它们的结构完全是有组织的,例如树、列表或链表。