You are on page 1of 23

Chapter Six

Data structures
Introduction
 The term data structure refers to a data collection with well-defined operations
and behavior or properties.
 A data structure is a unique way of storing or organizing the data in computer
memory so that we can use it effectively.
 The need for Data Structures in Java
Processing Speed: As the data is increasing day by day, high-speed processing
is required to handle this massive amount of data, but the processor may fail to
deal with that much amount of data.
 Searching data: Consider an inventory with a size of 200 items. If your
application needs to search for a particular item, it needs to traverse 200 items
in every search. This results in slowing down the search process.
 Multiple requests at the same time: Suppose, millions of users are
simultaneously searching the data on a web server, then there is a chance of
server failure.
Classification of Data Structures
 Linear Data Structures: In a linear data structure all the
elements are arranged in the linear or sequential order.
 The linear data structure is a single level data structure.
 Non-Linear Data Structures: The non-linear data
structure does not arrange the data in a sequential manner
as in linear data structures.
 Non-linear data structures are the multilevel data
structure.
The Set
 Set is a data structure that is used as a collection of
objects.
 Java supports it through its Java Collection library.
 There are several ways to implement a set and Java
supports three different implementations along with
useful operations like intersection between sets.
Cont.… The Set
 Key ideas:
 A set is a collection of objects like integers
 It allows insertion of elements in the set and to search an element
 Java set is an interface which extends collection.
 set can not contain duplicate elements .
 Java Set allow at most one null value.
 Set interface contains only methods inherited from Collection.
 Set provides basic Set operation like union, intersection and difference
between sets as well
Set Implementation Classes
 Java Provide three general purpose Set
implementations :
 HashSet: using a hash map behind the set
 TreeSet: using a tree behind the set
 LinkedHashSet: using a linked list as a collison
management technique in the hash map which is used
behind the set
Set Implementation Classes-2
Set Implementation Classes-3
 To define a HashSet with integer data types, use the
following code:
 Set<Integer> s = new HashSet<Integer>();
 To define a TreeSet with integer datatypes, use the
following code:
 Set<Integer> ts = new TreeSet<Integer>();
 To define a LinkedHashSet with integer datatypes, use the
following code:
 Set<Integer> ls = new LinkedHashSet<Integer>();
The List
 "List" is an abstract data type (ADT) – consisting of a
sequence of objects and operations on them.
 List in Java provides the facility to maintain the ordered
collection. It contains the index-based methods to insert,
update, delete and search the elements. It can have the
duplicate elements also.
Cont.… The List
 Singly-linked list
 A singly-linked list is a linked list that stores data and the reference to the
next node or a null value.
 Singly-linked lists are also known as one-way lists as they contain a node
with a single pointer pointing to the next node in the sequence.
 Doubly-linked list
 It is the same as a singly-linked list with the difference that it has two
pointers, one pointing to the previous node and one pointing to the next
node in the sequence.
 Circular Linked List
 In the Circular Linked List, all the nodes align to form a circle.
 In this linked list, there is no NULL node at the end.
 Circular linked lists are useful in implementing a circular queue.
List Implementation Classes
 There are two general purpose List implementations  
ArrayList and
LinkedList.
 If you frequently add elements to the beginning of
the List or iterate over the List to delete elements from its
interior, you should consider using LinkedList.
 These operations require constant-time in
a LinkedList and linear-time in an ArrayList.
The Queue
 a QUEUE is a specialized LIST in which insertions may
occur only at a designated position (the back) and
deletions may occur only at a designated position (the
front).

 There are two ends in the queue collection, i.e., front &
rear.
 Queue has two ends that is front and rear.
Cont.… The Queue
Cont.… The Queue
Queue Implementation Classes
 Classes used in implementation of Queue
 The classes that are used to implement the functionalities
of the queue are given as follows:
 ArrayDequeue
 LinkedList
 PriorityQueue


The STACK
 A STACK is a LIST in which insertions and deletions can
occur relative to just one designated position (called the
top of the stack).
Cont.…. The STACK
Implementation of STACK Using an Array
Implementation of STACK Using a LinkedList
Map/ dictionary
 A map contains values on the basis of key, i.e. key and
value pair. Each key and value pair is known as an entry.
 A Map contains unique keys.
 A Map is useful if you have to search, update or delete
elements on the basis of a key.
 There are two interfaces for implementing Map in java:
Map and SortedMap, and three classes:
 HashMap, LinkedHashMap, and TreeMap.
 The hierarchy of Java Map is given

below:
Cont… Map/ dictionary
Map/ dictionary
23 By Abdu A; October 7, 2022

You might also like