Java Abstract Data Type in Data Structure – ADT

Free Java courses with 37 real-time projects - Learn Java

Abstract Data types in Java are the most conceptual thing to learn in Java. It brings out the beauty of Java and its abstract implementation. Before we start to read about the topic in detail, let us take a real-life example of Abstract Data type in Java.

Put yourself inside the cockpit of a plane. You are Captain John Wick and you are in charge of flying the Boeing 777 from Delhi to London. Years of flight school and experience have sharpened your skills as a pilot and you are ready to take off. However, to fly a plane, do you need to know how the engines work? Do you know how the fuel discharge system works in an aircraft? No. You just need to know how to fly it.

This is an abstraction. In Java, you can implement abstraction by using classes and interfaces.

 

Abstract Datatypes in Java

Abstract Datatypes are the datatypes where you can logically work with the datatype, but you would not know the inner workings of the datatype. There are many different types of abstract data types in Java. We will talk about them in this article. Simply speaking, if you define a datatype with the help of a programming language and then hide its implementation, then it is an abstract datatype.

You just present the user with the datatype and the operations for that datatype. This enables the programmer to easily use complex data structures without thinking about its implementation.

Operations on Abstract Datatypes in Java

 

1. Creators – These operations primarily create objects of a certain type. However, note that some creators can take objects as parameters too. But a creator will never take an object of the same type as itself.

2. Producers – This may sound similar to the creator operations but producers produce a different object of the same type. It does this by performing some kind of operation on the objects and returning a new object. For example, when you merge two strings together, the producer method combines the two string objects and returns a new object.

3. Observers – These are essentially the operations that take an object of a single type and return an object of a different type. For example, if you want to check whether a stack is empty, the function takes a stack object and returns a boolean value.

4. Mutators – These operations change the object itself. This is evident when you add a new item to a list that changes the object itself.

List Abstract Datatype in Java

You must have heard about ArrayLists, which are basically dynamic arrays in Java. If you have not, do not worry they are very easy to master. If you have practiced designing lists in Java, you must have seen that the work of devising a new class each time is actually very tedious. Also, if you are a competitive programmer, implementing a linked list class for every program is tedious. This is why there is an Interface, namely the List interface. This interface would help us declare instances of Arraylists, lists, and so on.

Many classes, such as the LinkedList Class, the ArrayList class, all implement the list interface.

Operations on List in Java

We can perform a number of operations on a list.

1. get()– This method returns an element at the specified position

2. insert()– This method inserts an element of the same type as the rest of the list into the list. Note that you can insert this element in the list in any position.

3. remove()- This method removes the first occurrence of the element in the array. However, the array has to be non-empty.

4. removeAt()– This element removes the element at the given position in the non-empty list.

5. replace()- This replaces any element by the given element in the list.

6. size()– This returns the n number of elements present in the list.

7. isEmpty()– This function returns a boolean value to represent if the list is empty or not.

8. isFull()– This function is similar to the isEmpty() function . It returns true if the list is full and false if empty.

Stack Abstract Datatype in Java

When we were studying lists we came to know that the data are stored in the nodes of the datatype. In Stack ADT, instead of the actual data, the nodes store pointers to the data. This structure also stores the count of the number of elements, the maximum size of the stack, and the current node at the TOP of the stack.

Functions in Stack Abstract Data Type in Java

Some of the functions that we need to know are:

  • push()- This function is useful for pushing an element on the top of the stack.
  • pop()– This function is useful for popping/removing the top element of the stack.
  • peek()- This function returns the top element of the stack but does not pop it from the stack.
  • size()– This returns the number of elements present in the stack.
  • isEmpty()– As the name suggests, this function returns true if the stack is empty and false if it is not.
  • isFull()- Similarly this function returns a boolean value based on whether the stack is full or not.

Queue Abstract Data Type in Java

Queues are another abstract data type in Java. The design of this datatype is similar to the stack data type we saw earlier. Each of these nodes contains two pointers. One to the data in it and one for the node next to it. You must observe that this is similar to the linked lists that we studied about earlier.

In queues, the operations take place at both ends of the structure. This means that in queues, we can add elements at the rear of the data structure and remove elements from the front of it.

Methods in Queue Abstract Data Type in Java

Some important methods in queue datatype are:

  • enqueue()– This method is particularly useful for adding elements to the queue. These elements which are new in the queue area are added to the rear end of it.
  • dequeue()– This function removes an element from the front of the queue and returns it. However, if the queue is empty it does not return a proper value.
  • peek()- This function returns the topmost element of the queue. However, unlike the dequeue() method, it does not remove the element.
  • size()– This function returns the size of the queue.
  • isFull()– This function returns true if the queue is full and false if empty.
  • isEmpty()- Returns a boolean value based on whether the queue is empty or not.

Which Java ADT Should You Choose?

This is actually a very good question. If you already have this question then do not worry we are going to clear that out. So this question revolves around the question that you want to solve.

1. Use the list ADT when there is a linear relationship between your elements. Lists also facilitate O(1) deletions and retrieval. So try and use that to your advantage. This means that lists are useful when you need to refer to each element in your collection by an index.

2. Stacks ADT are particularly useful for designing solutions to problems that need the latest processed data first. Simply speaking, the stack data structure has a LIFO structure. Hence you can use it whenever you see that you need to implement a recursive solution without using recursion or something of that sort.

3. Queue ADT is useful when you need to implement some kind of discipline to the data items in the collection. For example, if your data items need to be processed in the order in which they arrived in the collection, you can use queues. operating systems use queues for implementing scheduling algorithms.

Quiz on Abstract Data Type in Java

Summary

In this article, we learned about the various Abstract Data type in Java. We also came to know about the different use-cases where abstract data types are useful. You could implement queues using Linked Lists or by using Arrays. This is the power of abstraction. This enables the programmers to rely and focus on the concept rather than fixating on the intricate working details of the program.

Did we exceed your expectations?
If Yes, share your valuable feedback on Google

follow dataflair on YouTube

2 Responses

  1. Efe Umurhohwo says:

    Nice write up. I gained from it. Thanks

Leave a Reply

Your email address will not be published. Required fields are marked *