Site icon TechVidvan

Java Abstract Data Type – Getting One Step Ahead in your Java Journey

Java Abstract Data Type

In our last articles, we discussed the different Data Structures in Java. In this article, we will learn the Abstract Data Type in Java which specify the Data structures. We will cover various Abstract Data Types such as List ADT, Stack ADT, and Queue ADT in detail.

So let’s begin with an introduction to Abstract Data Type (ADT).

Before that, it is recommended for you to take a quick revision of Data Types in Java to clear your basics with Techvidvan.

What is an Abstract Data Type in Java?

We know that a data type signifies the type and space taken by the data used in programs. An Abstract Data Type is a special data type that is defined by a set of values and a set of operations on that type.

We call these data types as “abstract” because these are independent of any implementation. We can use these data types and perform different operations with them, but we do not know how these operations are working internally.

The implementation details of these data types are totally invisible to the users. It does not specify how the data stores in the memory area and what algorithms are useful for implementing the operations on the data.

An Abstract Data Type in Java is useful in the implementation of data structures. Java library provides various Abstract Data Types such as List, Stack, Queue, Set, Map as inbuilt interfaces that we implement using various data structures.

WAIT! It’s the right time to get familiar with the concept of Data Structure in Java with Techvidvan.

Types and Operations of Java Abstract Data Type

Types

We can classify the Abstract data types either as built-in or user-defined or as mutable or immutable.

If an Abstract Data Type is mutable then we can change the objects of its type and if it is immutable then we can’t change its object.

For example, the Date class is mutable because we can call its setMonth() method and observe the change with the getMonth() operation. But String is immutable because its operations do not change the existing objects but create new String objects

Operations

There are following types of operations of an abstract type:

Java Abstract Data Type Examples

Below are some examples of abstract data types, along with some of their operations and the types.

1. int is a primitive integer type of Java. int is immutable, so it has no mutators. Its operations are:

Check out the different types of Operators in Java with Techvidvan.

2. The list is an interface of Java List. The list is mutable. Its operations are:

3. A string is Java’s string type. The string is immutable. Its operations are:

Get familiar with the Methods of Java Strings in detail with Techvidvan.

List of Java Abstract Data Type

Now, Let’s start exploring different Java Abstract Data Types in Java:

1. List ADT

The List Abstract Data Type is a type of list that contains similar elements in sequential order. The list ADT is a collection of elements that have a linear relationship with each other. A linear relationship means that each element of the list has a unique successor.

The List ADT is an interface, that is, other classes give the actual implementation of the data type. For example, Array Data Structure internally implements the ArrayList class while the List Data Structure internally implements the LinkedList class.

The List interface of Java library specifies 25 different operations/methods. Following are some of the operations that we can perform on the list:

2. Stack ADT

A stack is a LIFO (“Last In, First Out”) data structure that contains similar elements arranged in an ordered sequence. All the operations in stack take place at the top of the stack.

The below diagram shows the whole structure of the Stack ADT:

We can perform the following operations on the stack –

3. Queue ADT

A Queue is a FIFO (“First In, First Out”) data structure that contains similar types of elements arranged sequentially. We can perform the operations on a queue at both ends; insertion takes place at rear end deletion takes place at the front end.

Queue ADT is a collection in which the arrangement of the elements of the same type is in a sequential manner.

Operations performed on the queue are as follows:

Designing an Abstract Data Type in Java

To design an abstract data type we have to choose good operations and determine how they should behave. Here are a few rules for designing an ADT.

Which Java Abstract Data Type to choose?

Now after having brief knowledge of Java Abstract Data Types, we will discuss the scenarios to choose between either of List, Stack or Queue ADT.

List ADT is a collection of elements and stores them sequentially and which we can access using their indices. We can opt for this ADT in cases that involve indexed or sequential access or removal of elements.

For example, we can use various implementations of List ADT to store data of a list of employees in sorted order for sequential access or removal.

A Stack is a Last In First out data structure, and thus we can use implementations of Stack ADT in scenarios where we need to firstly access the most recently inserted elements.

For example, the function call stack of every programming language has the common requirement of this kind of LIFO data structure where there is a need to execute the most recent function in the stack.

The queue is a First In First Out data structure and we can choose the Queue ADT in scenarios where we need to access the elements in their order of insertion.

For example, one such scenario is request handling by web servers. Web servers make it possible to ensure the fairness of request handling according to their order of arrival by maintaining an internal queue for the requests.

Summary

That was all about Java Abstract Data Types. In this Java tutorial, we learned the basic concept of Java Abstract Data Type (ADT) and the types of Abstract Data Types in Java: List ADT, Stack ADT, and Queue ADT.

We discussed these Abstract Data Types in detail, along with their methods. In the end, we also discussed which Abstract Data Type we should choose while working with real-time applications.

Enhance your knowledge and get to know – What really differentiates an Abstract Class and Interface in Java? 

Thank you for reading our article. Do share our article on Social Media.

Happy Learning 🙂

Exit mobile version