Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java: Difference Between a collection and 'Data Structure' [closed]

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?

like image 730
user1888243 Avatar asked Jan 20 '13 02:01

user1888243


People also ask

Is collection framework of Java is a data structure?

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.

What is the difference between collection and array in Java?

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.

What is the difference between collection and stream in Java?

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.

What is the difference between collection and map in Java?

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.


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.

like image 109
Siddharth Avatar answered Oct 20 '22 12:10

Siddharth


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.

like image 32
Jeff Storey Avatar answered Oct 20 '22 12:10

Jeff Storey


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.

like image 1
Rob Avatar answered Oct 20 '22 12:10

Rob