Oops Report
Oops Report
AIM:
To study about different collection frameworks available in java.
1.Linked List:
A linked list is a linear data structure in which elements, known as
nodes, are interconnected using pointers. Each node typically
comprises two components:
1. Data: The actual value or information stored in the node.
2. Reference (or Pointer): A link to the next node in the sequence.
Key Characteristics
• Dynamic Size: Unlike arrays, the size of a linked list can grow or
shrink dynamically, making it suitable for situations where the
number of elements is unpredictable.
• Efficient Insertion/Deletion: Adding or removing elements is
faster compared to arrays, as it does not necessitate shifting
elements.
Types of Linked Lists
1. Singly Linked List: Each node points to the next node, and the
last node points to null.
2. Doubly Linked List: Each node contains two pointers, one to the
previous node and one to the next node.
3. Circular Linked List: The last node links back to the first node,
forming a circular structure.
Advantages
• Efficient Insertion and Deletion: Particularly useful for dynamic
datasets where elements are frequently added or removed.
• Memory Efficient for Variable Sizes: Saves memory when the
dataset size is variable, as it doesn't require pre-allocation.
Disadvantages
• No Random Access: You must traverse the list sequentially from
the head to reach a specific node, which can be time-
consuming.
• Higher Memory Overhead: Requires additional memory for
storing pointers.
Operations on Linked List:
Importing LinkedList:
Import java.util.LinkedList;
With Generics:
LinkedList<String> books = new LinkedList<>();
Without Generics:
LinkedList books = new LinkedList();
Adding Elements
books.add("Moby Dick"); books.add("1984");
Accessing Elements:
System.out.println(books.get(0));
• This gets the book stored at index 0.
Modifying Elements:
books.set(0, "Brave New World");
Removing Elements:
• By index: books.remove(0);
• By element: books.remove("Moby Dick");
import java.util.LinkedList;
Operations on HashMap:
Importing HashMap:
import java.util.HashMap;
With Generics:
HashMap<String, String> groceries = new HashMap<>();
Without Generics:
HashMap groceries = new HashMap();
Adding Elements:
groceries.put("Apple", "Fruit");
Accessing Elements:
System.out.println("Value for apple: " + groceries.get("Apple"));
Modifying Elements:
groceries.put("Apple", "Red Fruit");
Removing Elements: :
groceries.remove("Apple");
Without Lambda Expression
for (String key : groceries.keySet()) {
System.out.println(key + ": " + groceries.get(key));
}
With Lambda Expression
groceries.forEach((item, quantity) -> System.out.println(item + " -> "
+ quantity));
Checking if a Key Exists:
if (groceries.containsKey("Banana")) {
System.out.println("Banana exists in the HashMap.");
}
Clearing the HashMap:
groceries.clear();
System.out.println("After clearing the HashMap: " + groceries); //
Output: {}
Checking the Size of the HashMap:
System.out.println("Size of the HashMap: " + groceries.size());
Implementation of a Hashmap with Generics and
Lambda Expressions:
import java.util.HashMap;
// Accessing Elements
System.out.println("Quantity of 1984: " + books.get("1984"));
// Modifying Elements
books.put("1984", 12); // Update quantity
System.out.println("Updated books: " + books);
// Accessing Elements
System.out.println("Quantity of 1984: " + books.get("1984"));
// Modifying Elements
books.put("1984", 12); // Update quantity
System.out.println("Updated books: " + books);
// Adding Elements
books.add("Moby Dick");
books.add("1984");
books.add("To Kill a Mockingbird");
// Accessing Elements
System.out.println("Books in the collection:");
books.forEach(book -> System.out.println(book)); // Using
Lambda Expression
// Removing an Element
books.remove("1984");
System.out.println("After removing 1984, books in the
collection: " + books);
// Adding Elements
books.add("Moby Dick");
books.add("1984");
books.add("To Kill a Mockingbird");
// Accessing Elements
System.out.println("Books in the collection:");
books.forEach(book -> System.out.println(book)); // Using
Lambda Expression
// Removing an Element
books.remove("1984");
System.out.println("After removing 1984, books in the
collection: " + books);
Operations on TreeSet :
Importing TreeSet:
import java.util.TreeSet;
With Generics:
Without Generics:
Adding Elements:
books.add("Moby Dick");
Accessing Elements:
Removing Elements:
books.clear();
System.out.println("After clearing, size of the TreeSet: " +
books.size());
Implementation of Treeset with Generics and Lambda
Expressions:
import java.util.TreeSet;
// Adding Elements
books.add("Moby Dick");
books.add("1984");
books.add("To Kill a Mockingbird");
// Accessing Elements
System.out.println("First Book: " + books.first());
// Removing an Element
books.remove("1984");
System.out.println("After removing 1984, books in the
collection: " + books);
// Adding Elements
books.add("Moby Dick");
books.add("1984");
books.add("To Kill a Mockingbird");
// Accessing Elements
System.out.println("First Book: " + books.first());
Operations on Vector:
Importing Vector:
import java.util.Vector;
With Generics:
Vector<String> books = new Vector<>();
Without Generics:
Vector books = new Vector();
Adding Elements:
books.add("Moby Dick");
Accessing Elements:
System.out.println("First Book: " + books.get(0)); // Access by index
Modifying Elements:
books.set(0, "Brave New World"); // Update the first element
Removing Elements:
books.remove("1984"); // Removes the element "1984"
Iterating without Lambda Expression:
for (Object book : books) {
System.out.println(book);
}
Iterating with Lambda Expression:
books.forEach(book -> System.out.println(book));
Checking for an Element:
if (books.contains("To Kill a Mockingbird")) {
System.out.println("To Kill a Mockingbird is in the collection.");
}
Clearing the Vector:
books.clear(); System.out.println("After clearing, size of the
collection: " + books.size()); // Output: 0
Implementation of Vector with Generics and Lambda
Expressions:
import java.util.Vector;
RESULT:
The study on different collection classes are done successfully.