Top 5 Data Structures in Java

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 are 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, LinkedList, Stack, Queue, Map, Set and How they are implemented in Java, along with how to use them.


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 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 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 best use of them.

Array

Java programming language provides builtin support for array in language itself. It has special syntax to declare array e.g. int[] , which is array of int primitive type. You can create array of both reference type and primitives. Also unlike C programming language, array in Java are bounded and you will get ArrayIndexOutOfBoundException if you are working with invalid index. Array in Java are also homogeneous, you can not store multiple type of object in a 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 following tutorial to print array, compare two arrays and find duplicates in array.

Linked List

Data Structures in Java Programming, Array, List, Queue, StackApart from array, 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 implement Collection and Iterable interface as well, which allows to iterate over them. You can check this article to learn more about LinkedList in Java

Stack

Java API also provide a Stack data structure implementation as java.util.Stack. This class extends legacy Vector class for storing elements. Since stack is a LIFO (Last In, First out) data structure, it provides push() method to insert objects and pop() method to consume elements from top. 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.

Queue

Queue data structure is also available in Java collection framework as interface and few concrete implementation 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 reduce development time but also overall code quality and performance of your application. BlockingQueue is thread safe extension of Queue interface and can be used to implement produce consumer pattern in Java.

Map

Map is one of the most versatile data structure I have seen. I happen to use Map every now and than and fortunately Java API provides several implementation of Map data structure for different needs e.g. HashMap, Hashtable and ConcurrentHashMap. Map provides you O(1) functionality for getting a value back, if you know the key, which is very natural use case in most of java application.

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 couple of implementation of Set e.g. HashSet,TreeSet and LinkedHashSet, which is more than enough for most of situations. Those collection, apart form begin set also provides sorting and insertion order.


That's all on this tutorial about data structure in Java. Apart from these basic data structure, there are lot more in 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.

Other Java Collection tutorials from Javarevisited blog

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