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?
In Java, dynamically allocated data structures (such as ArrayList , LinkedList , Vector , Stack , HashSet , HashMap , Hashtable ) are supported in a unified architecture called the Collection Framework, which mandates the common behaviors of all the classes.
Arrays can hold only homogeneous data types elements. Collection can hold both homogeneous and and heterogeneous elements. There is no underlying data structure for arrays and hence ready made method support is not available.
Streams are not modifiable i.e one can't add or remove elements from streams. These are modifiable i.e one can easily add to or remove elements from collections. Streams are iterated internally by just mentioning the operations. Collections are iterated externally using loops.
Map is a more specific version of a Collection that has a Key -> Data structure. Collection is just the interface that has data structures for storing data in Java.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With