0% found this document useful (0 votes)
16 views10 pages

Chapter 6

Uploaded by

Shafi Esa
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views10 pages

Chapter 6

Uploaded by

Shafi Esa
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 10

Chapter 6: Data Structures in

Object-Oriented
Programming
In this chapter, we will explore various essential
data structures that play a crucial role in
organizing and managing data in OOP. Each sub-
lesson focuses on a specific data structure, its
characteristics, and implementation classes.
6.1. The Set
• A set is a collection of distinct elements with no specific order. In OOP,
sets are often used to store unique values and provide efficient
operations for set manipulation.
Key Concepts:
• Uniqueness of elements
• Set operations: union, intersection, difference
import java.util.HashSet; // Displaying elements
import java.util.Set; System.out.println("Set elements: " +
set); // Set operations
public class SetExample {
Set anotherSet = new HashSet<>();
public static void main(String[]
anotherSet.add("Banana");
args) {
anotherSet.add("Grapes"); // Union
// Creating a HashSet set.addAll(anotherSet);
Set<String> set = new HashSet<>(); System.out.println("Union: " + set);
// Adding elements to the set // Intersection set.retainAll(anotherSet);
set.add("Apple"); System.out.println("Intersection: " + set);
set.add("Banana"); // Difference set.removeAll(anotherSet);
System.out.println("Difference: " +
set.add("Orange"); set); } }
6.2. Set Implementation Classes
HashSet:
• Implements a set using a hash table for efficient element retrieval.
• O(1) average time complexity for basic operations.
TreeSet:
• Implements a set using a self-balancing binary search tree.
• Provides ordered iteration of elements.
• O(log n) time complexity for basic operations.
6.3. The List
• A list is an ordered collection of elements, where each element has a position or
index. Lists are fundamental for storing and managing sequential data.
Key Concepts:
• Ordered elements with index access
• Dynamic size
6.4. List Implementation Classes
ArrayList:
• Implements a resizable array.
• Efficient for random access but less so for insertions and deletions.
LinkedList:
• Implements a linked list.
• Efficient for insertions and deletions but slower for random access.
import java.util.ArrayList; // LinkedList example
import java.util.LinkedList; List<String> linkedList = new LinkedList<>();
import java.util.List; linkedList.add("Node 1");
public class ListExample { linkedList.add("Node 2");
public static void main(String[] args) { linkedList.add("Node 3");
// ArrayList example // Displaying elements
List<String> arrayList = new System.out.println("ArrayList: " + arrayList);
ArrayList<>(); System.out.println("LinkedList: " + linkedList)
arrayList.add("Element 1"); }
arrayList.add("Element 2"); }
arrayList.add("Element 3");
6.5. The Queue

A queue is a data structure that follows the First-In-First-Out (FIFO) principle, where
the first element added is the first to be removed.
Key Concepts:
• Enqueue and dequeue operations
6.6. Queue Implementation Classes
ArrayQueue:
• Implements a queue using a resizable array.
• Efficient for basic queue operations.
LinkedQueue:
• Implements a queue using a linked list.
• Efficient for dynamic size and frequent enqueue and dequeue operations.
import java.util.ArrayDeque; // LinkedList example (implements Queue
interface)
import java.util.LinkedList;
Queue<String> linkedListQueue = new
import java.util.Queue; LinkedList<>();
public class QueueExample { linkedListQueue.add("Job 1");
public static void main(String[] args) { linkedListQueue.add("Job 2");
// ArrayDeque example (can be used linkedListQueue.add("Job 3");
as a queue) // Displaying elements
Queue<String> arrayDequeQueue = System.out.println("ArrayDeque Queue: " +
new ArrayDeque<>(); arrayDequeQueue);
System.out.println("LinkedList Queue: " +
arrayDequeQueue.add("Task 1"); linkedListQueue);
arrayDequeQueue.add("Task 2"); }
arrayDequeQueue.add("Task 3"); }
6.7. Map/Dictionary
• A map, also known as a dictionary, is a collection of key-value pairs. Each key must be unique, and it
maps to a specific value.
Key Concepts:
• Key-value pairs
• Efficient key-based retrieval
Implementation Classes
HashMap:
• Uses a hash table for efficient key-based operations.
• O(1) average time complexity for basic operations.
TreeMap:
• Implements a map using a self-balancing binary search tree.
• Provides ordered key iteration.
• O(log n) time complexity for basic operations.
import java.util.HashMap; // TreeMap example
import java.util.Map; Map<String, Integer> treeMap = new
import java.util.TreeMap; TreeMap<>();
public class MapExample { treeMap.put("Apple", 10);
public static void main(String[] args) { treeMap.put("Banana", 5);
// HashMap example treeMap.put("Orange", 8);
Map<String, Integer> hashMap = // Displaying elements
new HashMap<>(); System.out.println("HashMap: " +
hashMap.put("One", 1); hashMap);
hashMap.put("Two", 2); System.out.println("TreeMap: " + treeMap
hashMap.put("Three", 3); }

You might also like