What are Java 8 Data Structures and How to use them?

Reading Time: 3 minutes

What is Data Structure

A data structure is a particular way of organizing data in a computer so that it can be used effectively.

Basic types of Data Structure in Java

  • Arrays

The array is a fixed-size data structure with random access. Fixed-size means that while creating an array we need to specify its size(number of elements array can hold). It has a special syntax to declare an array like int[], which is an array of primitive int types. You can create an array of both reference types and primitives. As shown below in the example.

  • Linked List

The linked list is a dynamic size collection. The way linked list achieved its dynamism is by storing its elements all over the memory(not together as an array) and linking them one to another. Java provides a doubly-linked list implementation as java.util.LinkedList, this class can be used whenever a linked list data structure is needed.

Since LinkedList is part of the Collection framework, it implements Collection and Iterable interface as well, which allows iterating over them.

The main disadvantage — there is no random access. In order to get some value, we need to travel from the beginning to a specific element. There is no way I can in one step get a specific element if it’s not the first one.

  • Stack

Java API also provides a Stack data structure implemented as java.util.Stack. This class extends the legacy Vector class for storing elements. Since the stack is a LIFO (Last In, First Out) data structure, it provides a push() method to insert objects and a pop()= method to consume elements from the top.

  • Queue

The queue is an abstract data type that depicts First in first out (FIFO) behavior. The queue data structure is also available in the Java collection framework as an interface and a few concrete implementations like ArrayBlockingQueue, LinkedList, and PriorityQueue. Though we can also implement Queue by using LinkedList or array, it’s much better to use existing classes, which are tried and tested.

Knoldus-blog-footer-image

Written by 

Sumit Agarwal is a Software Consultant having more than 1+ years of experience. He is good at problem-solving skills. He likes to watch football and cricket. He has mainly experience in Java and Mysql.