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?
-
Although simple question, but glad you asked. Many don't ask and remain in doubt forever :-) – CuriousMind Dec 07 '17 at 17:49
3 Answers
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.
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.
Your confusion is between internal storage and accessing the storage. Separate the 2.

- 9,349
- 16
- 86
- 148
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.

- 56,312
- 72
- 233
- 406
-
Does the term collection have meaning in other programming languages, or it is really a term that Java uses? – user1888243 Jan 20 '13 at 02:34
-
1It's a generic term. But when used in the Java context, it usually means the java collection classes. – Jeff Storey Jan 20 '13 at 02:35
-
1+1 And also, by that definition, an implementation of the `Collection` interface is a data structure that provides methods for dealing with a group of objects. – Marc Baumbach Jan 20 '13 at 02:35
-
2There is one noteable exception of a Java collection type which does not implement Collection however: Map. – fge Jan 20 '13 at 02:39
-
Another notable exception in Java is the 3rd-party class (like the Trove collection classes) that provide collection functionality *without* using `java.util.Collection`. https://bitbucket.org/trove4j/trove – Stephen C Jan 25 '16 at 23:13
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.

- 11,446
- 7
- 39
- 57