Java: Difference Between a collection and 'Data Structure'
Solution 1
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.
Solution 2
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.
Solution 3
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.
user1888243
Updated on July 05, 2022Comments
-
user1888243 almost 2 years
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?
-
user1888243 over 11 yearsDoes the term collection have meaning in other programming languages, or it is really a term that Java uses?
-
Jeff Storey over 11 yearsIt's a generic term. But when used in the Java context, it usually means the java collection classes.
-
Marc Baumbach over 11 years+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. -
fge over 11 yearsThere is one noteable exception of a Java collection type which does not implement Collection however: Map.
-
Stephen C over 8 yearsAnother notable exception in Java is the 3rd-party class (like the Trove collection classes) that provide collection functionality without using
java.util.Collection
. bitbucket.org/trove4j/trove