6 Data Structures Every Java Programmer Should Know

Java API provides built-in support for common data structures, essential for writing programs e.g. array, linked list, map, set, stack and queue. You don't need to implement these data structure by yourself, you can directly use it in your program, thanks to rich and efficient implementation provided by Java API. This is also one reason, why Java is the best programming language. Since data structure is core to any program and choice of a particular data structure greatly affects both functionality and performance of Java applications, it's worth an effort to explore different data structure available in Java.

Many of these data structure is part of hugely popular Java Collection Framework, and almost all Java programs may be, except hello world make use of Collection in one or another form. 

In this Java tutorial, we will take a look at common data structure e.g. Array, linked list, Stack, Queue, Map, Set and How they are implemented in Java, along with how to use them.

Btw, if you are a complete beginner in the world of data structure and algorithms, then I also suggest you to first go through a comprehensive course like Data Structures and Algorithms: Deep Dive Using Java to learn basics and master it.

Data Structure and Algorithms are key for becoming a better developer and any investment you make in terms of your time, money and learning effort will pay you for a long time to come.




6 Essential Data Structure in Java

Here is my list of the fundamental data structure from standard Java API and programming language itself, since an array is part of the programming language itself while others are part of the popular Java Collection framework.

With Java 8 coming up with Lambda expression, Functional Interface, and Streams, which is going to give a new life to Java Collection Framework, especially in the context of leveraging multiple core architecture of modern CPU.

It’s high time that beginners make themselves aware of basic data structure available in Java programming and make the best use of them.


1. Array

Java programming language provides built-in support for the array in the language itself. It has a special syntax to declare array e.g. int[], which is an array of int primitive type. You can create an array of both reference type and primitives. 

Also unlike C programming language, an array in Java is bounded and you will get ArrayIndexOutOfBoundException if you are working with an invalid index. 

Array in Java are also homogeneous, you can not store multiple types of object in an array e.g. you can only store a String in a String[], if you will try to store Integer, you will get ArrayStoreException at runtime. 

You can check further check Data Structures and Algorithms: Deep Dive Using Java to learn more about array data structure and how to use it in Java.

array data structure in Java



2. Linked List

Apart from the array, a linked list is another basic data structure in programming. 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 Collection framework, it implements Collection and Iterable interface as well, which allows iterating over them. You can check this article to learn more about LinkedList in Java

linked list data structure in Java



3. Hash table

The Hash table, map or dictionary is one of the most versatile data structure I have seen. I happen to use Map every now and then and fortunately, Java API provides several implementations of Map data structure for different needs like HashMap, Hashtable, and ConcurrentHashMap.

It's also known as map or dictionary data structure, you might have heard about Dictionary in Python, which is the same as Map in Java. 

A map provides you O(1) functionality for getting a value back if you know the key, which is a very natural use case in most of the java application.

You can further check the Algorithms and Data Structures - Part 1 and 2 courses on Pluralsight to learn more about Hash table, map, or dictionary data structure in Java.


hash table or dictionary data structure in Java


4. 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 push() method to insert objects and pop() method to consume elements from the top. 

The stack is quite popular in different programming task e.g. evaluating expressions. By the way don’t confuse Stack data structure with stack memory, which is used to store local variable and method frames in Java.

Btw, if you are refreshing your data structure concepts for Interviews, I also suggest you go through the Data Structures in Java: An Interview Refresher course on Educative to prepare well for your interview.

stack data structure in Java




5. Queue

The queue data structure is also available in Java collection framework as interface and few concrete implementations e.g. ArrayBlockingQueue, LinkedList and PriorityQueue. 

Though you can also implement Queue by using LinkedList or array, it's much better to use existing classes, which are tried and tested. 

This not only reduces development time but also overall code quality and performance of your application. BlockingQueue is a thread-safe extension of Queue interface and can be used to implement produce consumer pattern in Java.


Queue data structure in Java


6. Set

Set is a special data structure, which doesn't allow duplicates. It's a good data structure to store unique elements e.g. IDs. Java Collection API provides a couple of implementation of Set like HashSet, TreeSet and LinkedHashSet, which is more than enough for most situations. Those collections, apart form begin set also provides sorting and insertion order.

Set data structure in Java



That's all about some of the most essential Data Structure for Java developers.  Apart from these basic data structures, there are a lot more in the Java collection framework, including concurrent data structure like BlockingQueue and ConcurrentHashMap. For a Java developer with any experience level, it's good to explore new collection classes introduced in Java 5 and 6 for making better use of Java API.


Further Learning
Data Structures and Algorithms: Deep Dive Using Java
Algorithms and Data Structures - Part 1 and 2
Introduction to Algorithms by Thomas H. Corman

Other Data Structure and Algorithms  You may like
  • 50+ Data Structure and Algorithms Problems from Interviews (list)
  • 5 Books to Learn Data Structure and Algorithms in depth (books
  • How to reverse an array in Java? (solution)
  • 75+ Coding Interview Questions for Programmers (questions)
  • How to remove duplicate elements from the array in Java? (solution)
  • How to implement a recursive preorder algorithm in Java? (solution)
  • How to implement a binary search tree in Java? (solution)
  • Post order binary tree traversal without recursion (solution)
  • How to print leaf nodes of a binary tree without recursion? (solution)
  • Recursive Post Order traversal Algorithm (solution)
  • Iterative PreOrder traversal in a binary tree (solution)
  • How to count the number of leaf nodes in a given binary tree in Java? (solution)
  • Recursive InOrder traversal Algorithm (solution)
  • 10 Free Data Structure and Algorithm Courses for Programmers (courses)
  • 100+ Data Structure Coding Problems from Interviews (questions)

Thanks for reading this article so far. If you like this Java Array tutorial then please share with your friends and colleagues. If you have any questions or feedback then please drop a comment.

P. S. - If you are looking for some Free Algorithms courses to improve your understanding of Data Structure and Algorithms, then you should also check the Easy to Advanced Data Structures course on Udemy. It's authored by a Google Software Engineer and Algorithm expert and its completely free of cost.


2 comments:

  1. By using Object[] array i can store homogeneous and heterogeneous data but main problem with array is size limitation.

    Thanks.

    ReplyDelete
    Replies
    1. hello Sriniva, the maximum length of array is Integer.MAX_VALUE because you can only use a int variable to specify length of array. If you need bigger cache than that, you probably need more than one array or sophisticated caching solution like EhCache.

      Delete