Data structure is a way of storing and organizing data. Data structure provides a way to process and store data efficiently. Choosing the right data structure in Java is crucial to efficiency and speed.
There are two types of data structures:
Primitive data structure
Non-primitive data structure
The primitive data structures are primitive data types. The int, char, float, double, and pointer are the primitive data structures that can hold a single value.
Non-primitive data structure is divided into two types:
Linear data structure
Non-linear data structure
Linear data structure means elements are sorted and searched sequentially, one after the other. There is one first element, one last element, and every element in between has a "next" and "previous."
Array
Linked-list
Stacks
Queues
Array is a linear data structure that stores a fixed number of similar elements. An array can store primitive data types and objects but they should be the same kind. This is one of the most used data structures in Java.
one dimensional array
two dimensional array
multi-dimensional array
You can randomly access array elements using index
It can represent multiple elements of same type with single name
You can implement various data structures such as LinkedList, Stack and Queue using Array
You need to know number of elements in advance.
You can not modify array once its size is defined
Insertion and deletion are quite costly operation in array as you need to shift elements.
Stack is abstract data type which depicts Last in first out (LIFO) behavior.
Stack manages the data in a LIFO method, which is possible with a linked list and array.
You can use Stack to manage how memory is allocated and deallocated.
Stack is more secure and reliable since it does not easily get corrupted.
Stack does not allow resizing of variables.
Stack overflow can occur if you create too many objects on the stack.
Stack memory is limited.
Data cannot be randomly accessed in a stack.
Queue is abstract data type which depicts First in first out (FIFO) behavior.
flexible and quick.
In addition, queues have the potential to be infinitely long, as opposed to fixed-length arrays.
A major disadvantage of a classical queue is that a new element can only be inserted when all of the elements are deleted from the queue.
In singly linked list, Node has data and pointer to next node. It does not have pointer to the previous node. Last node ‘s next points to null, so you can iterate over linked list by using this condition.
In doubly linked list, Node has data and pointers to next node and previous node.You can iterate over linkedlist either in forward or backward direction as it has pointers to prev node and next node. This is one of most used data structures in java.
A non-linear data structure is another important type in which data elements are not arranged sequentially; mainly, data elements are arranged in random order without forming a linear structure. Instead, it was arranged in a hierarchy.
tree
graph
heap
hash
Binary tree is not linear but rather, hierarchical. This means that the top-most element is the "root" of the tree, and all other nodes connect to it.
A binary tree is a tree data structure in which each node has at most two children, which are referred to as the left child and the right child.
The file system hierarchy of this structure allows for simple relationships among data to be easily represented. Plus, you can efficiently insert data and search the nodes with ease.
However, sorting data can be challenging, and it's quite inflexible. With that said, there are many variations of the binary tree, like the binary search tree.
The binary search tree is a special type of binary tree which have the following properties:
Nodes that are smaller than the root will be in the left subtree.
Nodes that are greater than the root will be the right subtree.
It should not have duplicate nodes.
Both left and right subtrees also should be binary search trees.
A graph is a data structure for storing connected data like a network of people on a social media platform. Vertices and edges make up a graph. The entity (for example, people) is represented by a vertex, and the relationship between entities (for example, a person's friendships) is represented by an edge.
The fundamental benefit of a graph data structure is that it allows you to use all graph-related computer science algorithms. You can use all of the power of graph algorithms to solve your problem once you've worked out how to represent your domain logic as a graph.
The main disadvantage is its large memory complexity.