CS & IT
ENGINEERING
Java With OOPS
Java Collections
Lecture No-01 By- Aditya sir
Recap of Previous Lecture
Topic Fundamentals of
Java
Datatypes
Operators treating
if else functions
stalemts
switch Jump
loop MIC
Topics to be Covered
Topic Java Collections
muchapter
1 8710
3
Topic : Introduction to Collections
Definition:
• Collections are data structures that group multiple elements into a single unit.
The Java Collections Framework (in [Link]) provides standard interfaces (List,
Set, Map) and their implementations.
Advantages Over Arrays:
• Dynamic Sizing: Automatically adjusts to the number of elements.
• Rich API: Methods for adding, removing, searching, sorting, etc.
• Type Safety: Use of Generics minimizes runtime type errors.
• Utility Methods: Built-in support for common algorithms (e.g., sorting).
Use Cases:
• Managing dynamic user inputs (shopping carts, to-do lists).
• Data-driven applications with frequently changing data sets.
Topic : Introduction to ArrayList
What is ArrayList?
• A resizable-array implementation of the List interface.
• Maintains insertion order and allows duplicate elements.
Core Methods:
• Addition: add(element) and add(index, element).
• Access: get(index).
• Update: set(index, element).
• Removal: remove(index) or remove(object).
• Other Methods: size(), contains(element), clear(), indexOf(element).
Features:
• Random access with index lookup.
• Ideal for dynamic, frequently modified data.
Topic : Basic ArrayList Operations
Example 1 – Creating and Adding Elements
• Problem Statement: Create an ArrayList of fruit names and print the list.
• Hint: Use add() to insert each fruit.
Topic : Basic ArrayList Operations
uh x
• Code:
import [Link];
import jara
public class FruitAdder {
public static void main(String[] args) {
ArrayList<String> fruits = new ArrayList<>();
[Link]("Apple");
[Link]("Banana");
[Link]("Cherry");
[Link]("Date");
[Link]("Elderberry");
[Link]("Fig");
[Link]("Grape");
[Link]("Honeydew");
[Link]("Fruit List: " + fruits);
}
}
Topic : Basic ArrayList Operations
Example 2 – Removing an Element
• Problem Statement: Create an ArrayList of integers and remove the element at
index 1.
• Hint: Use remove(index) to delete an element; the list will re-index automatically.
Topic : Basic ArrayList Operations
• Code:
import [Link];
public class RemoveNumber {
public static void main(String[] args) {
ArrayList<Integer> numbers = new ArrayList<>();
[Link](10);
[Link](20);
[Link](30);
[Link](40);
[Link](50);
[Link](1); // removes 20
[Link]("After removal: " + numbers);
}
}
Topic : Basic ArrayList Operations
Example 3 – Accessing an Element
• Problem Statement: Create an ArrayList of characters and print the element at
index 2.
• Hint: Use get(index) to retrieve an element by its position.
Topic : Basic ArrayList Operations
• Code:
import [Link];
public class AccessChar {
public static void main(String[] args) {
ArrayList<Character> letters = new ArrayList<>();
see
[Link]('A’);
[Link]('B’);
[Link]('C’);
[Link]('D’);
[Link]('E’);
[Link]("Element at index 2: " + [Link](2));
}
}
Topic : Basic ArrayList Operations
Example 4 – Updating an Element
• Problem Statement: Create an ArrayList of integers, update the element at index
0, and print the updated list.
• Hint: Use set(index, element) to modify an element's value.
Topic : Basic ArrayList Operations
• Code:
import [Link];
public class UpdateElement {
public static void main(String[] args) {
ArrayList<Integer> numbers = new ArrayList<>();
[Link](100);
[Link](200);
[Link](300);
[Link](400);
[Link](500);
[Link](0, 150); // update index 0 to 150
[Link]("Updated List: " + numbers);
}
}
Topic : Basic ArrayList Operations
Example 5 – Iterating with a For Loop
• Problem Statement: Create an ArrayList of integers and print each element using
a traditional for loop.
• Hint: Use size() and get(i) for iteration.
Topic : Basic ArrayList Operations
• Code:
import [Link];
public class IterateForLoop {
public static void main(String[] args) {
ArrayList<Integer> nums = new ArrayList<>();
[Link](10);
[Link](20);
[Link](30);
[Link](40);
[Link](50);
for (int i = 0; i < [Link](); i++) {
[Link]([Link](i) + " ");
}
}
}
Topic : Basic ArrayList Operations
Example 6 – Iterating with a For-Each Loop
• Problem Statement: Create an ArrayList of strings and print each element using a
foreach loop.
• Hint: The enhanced for loop simplifies traversal without using indices.
Topic : Basic ArrayList Operations
• Code:
import [Link];
public class IterateForEach {
public static void main(String[] args) {
ArrayList<String> names = new ArrayList<>();
[Link]("Alice");
[Link]("Bob");
[Link]("Charlie");
[Link]("Diana");
var Collect non
[Link]("Evan");
[Link]("Fiona");
For datatype
[Link]("George");
[Link]("Hannah");
for (String name : names) {
[Link](name);
}
}
}
Topic : Basic ArrayList Operations
Example 7 – Checking for an Element
• Problem Statement: Create an ArrayList of strings and check if it contains "Bob".
• Hint: Use contains() to verify if the list includes the given element.
Topic : Basic ArrayList Operations
• Code:
import [Link];
public class ContainsCheck { contains Bob
public static void main(String[] args) {
if names
ArrayList<String> names = new ArrayList<>();
[Link]("Alice");
[Link]("Bob");
[Link]("Charlie"); Sop Ñ b Tnum cortam
[Link]("Diana");
[Link]("Evan"); Bob
[Link]("Fiona");
[Link]("George");
[Link]("Hannah");
boolean found = [Link]("Bob");
[Link]("Contains 'Bob'? " + found);
}
}
Topic : Basic ArrayList Operations
Example 8 – Getting the Size of the ArrayList
• Problem Statement: Create an ArrayList of strings, add several color names, and
print the list size.
• Hint: Use the size() method to get the number of elements.
Topic : Basic ArrayList Operations
• Code:
import [Link];
public class ListSize {
public static void main(String[] args) {
ArrayList<String> colors = new ArrayList<>();
[Link]("Red");
[Link]("Green");
[Link]("Blue");
[Link]("Yellow”);
[Link]("Purple");
[Link]("Orange");
[Link]("Pink");
[Link]("Brown");
[Link]("List Size: " + [Link]());
}
}
Topic : Advanced ArrayList Operations
Example 1 – Inserting at a Specific Index
• Problem Statement: Insert "Bob" into an ArrayList of names at index 1 and print
the updated list.
• Hint: Use add(index, element) to insert without overwriting existing elements.
Topic : Advanced ArrayList Operations
• Code:
import [Link];
public class InsertElement {
public static void main(String[] args) {
ArrayList<String> names = new ArrayList<>();
[Link]("Alice");
[Link]("Charlie");
[Link]("David");
[Link]("Eve");
[Link]("Frank");
[Link]("Grace");
[Link]("Heidi");
[Link]("Ivan");
[Link](1, "Bob"); // Insert "Bob" at index 1
[Link]("After insertion: " + names);
}
}
Topic : Advanced ArrayList Operations
Example 2 – Converting an Array to an ArrayList
• Problem Statement: Convert an array of integers to an ArrayList and print the
result.
• Hint: Use [Link]() wrapped in a new ArrayList to create a dynamic list.
Topic : Advanced ArrayList Operations
• Code:
import [Link];
import [Link];
public class ArrayToList {
public static void main(String[] args) {
Integer[] numbers = {1, 2, 3, 4, 5};
ArrayList<Integer> numList = new ArrayList<>([Link](numbers));
[Link]("Converted ArrayList: " + numList);
}
}
Topic : Advanced ArrayList Operations
Example 3 – Sorting an ArrayList
• Problem Statement: Sort an ArrayList of integers in ascending order and print
the sorted list.
• Hint: Use [Link]() to sort the ArrayList naturally.
Topic : Advanced ArrayList Operations
• Code:
import [Link];
import [Link];
public class SortList {
public static void main(String[] args) {
ArrayList<Integer> numbers = new ArrayList<>();
[Link](50);
[Link](10);
[Link](40);
How to Sost
[Link](20);
[Link](30);
HI
[Link](60); in desc order
[Link](70);
[Link](80);
[Link](numbers);
[Link]("Sorted List: " + numbers);
}
}
Topic : Advanced ArrayList Operations
Example 4 – Reversing an ArrayList
• Problem Statement: Reverse the order of elements in an ArrayList of strings and
print the reversed list.
• Hint: Use [Link]() to reverse the order.
Topic : Advanced ArrayList Operations
• Code:
import [Link];
import [Link];
public class ReverseList {
public static void main(String[] args) {
ArrayList<String> words = new ArrayList<>();
[Link]("One");
[Link]("Two");
[Link]("Three"); HIMyTm
[Link]("Four");
[Link]("Five");
[Link]("Six");
[Link]("Seven");
[Link]("Eight");
[Link]("Reversed List: " + words);
}
} Collections sense coos
Topic : Advanced ArrayList Operations
Example 5 – Finding the Index of an Element
• Problem Statement: Find and print the index of "Tokyo" in an ArrayList of city
names.
• Hint: Use indexOf(element) to obtain the index of the first occurrence.
elem inde
Topic : Advanced ArrayList Operations
• Code:
import [Link];
public class FindIndex {
public static void main(String[] args) {
ArrayList<String> cities = new ArrayList<>();
[Link]("New York");
[Link]("Paris");
if
op
[Link]("Tokyo");
[Link]("London");
[Link]("Berlin");
[Link]("Sydney");
[Link]("Moscow");
int index = [Link]("Tokyo");
[Link]("Index
she
of Tokyo: " + index);
}
}
Topic : Advanced ArrayList Operations
Example 6 – Clearing an ArrayList
O
• Problem Statement: Clear all elements from an ArrayList and print the empty
list.
• Hint: Use the clear() method to remove all elements.
Topic : Advanced ArrayList Operations
import [Link];
public class ClearArrayList {
public static void main(String[] args) {
ArrayList<String> colors = new ArrayList<>();
[Link]("Red");
[Link]("Green");
[Link]("Blue”);
[Link]("Yellow");
[Link]("Purple");
[Link]("Orange");
[Link]("Pink");
[Link]("Brown");
[Link]("Black");
[Link]("White");
[Link]();
[Link]("Cleared List: " + colors);
}
}
Topic : Advanced ArrayList Operations
Example 7 – Extracting a Sublist
• Problem Statement: Extract a sublist from an ArrayList of integers (from index 1
to 4) and print it. o
• Hint: Use subList(fromIndex, toIndex) where toIndex is exclusive.
Q
Topic : Advanced ArrayList Operations
• Code:
import [Link];
import [Link];
public class SubListDemo {
public static void main(String[] args) { 1 - 5
ArrayList<Integer> numbers = new ArrayList<>();
[Link](10);
[Link](20); 5
[Link](30);
[Link](40);
[Link](50);
3
[Link](60); ex
[Link](70);
no
95
List<Integer> subList = [Link](1, 5); // indices 1,2,3,4
[Link]("Sublist: " + subList);
}
-
}
Topic : Advanced ArrayList Operations
Example 8 - Combining Two ArrayLists
• Problem Statement: Merge two ArrayLists of strings into one and print the
resulting list.
• Hint: Use addAll() to combine the elements of the second list into the first.
Topic : Advanced ArrayList Operations
• Code:
import [Link];
public class CombineLists {
public static void main(String[] args) {
ArrayList<String> list1 = new ArrayList<>();
[Link]("Red");
[Link]("Green");
[Link]("Blue");
ArrayList<String> list2 = new ArrayList<>();
[Link]("Orange");
[Link]("Yellow");
[Link]("Purple");
[Link](list2);
[Link]("Combined List: " + list1);
}
}
Topic : Recap and Best Practices for ArrayList
Theory Recap:
• Dynamic Sizing: ArrayLists automatically adjust their size based on the number
of elements.
• Core Methods: Common methods include add, remove, get, set, size, contains,
clear, indexOf, subList, and addAll.
• Iteration: Use both traditional for loops and enhanced for-each loops for iterating
over elements.
• Conversion: Easily convert arrays to ArrayLists using [Link]().
• Merging: Combine multiple ArrayLists using addAll()
Topic : Recap and Best Practices for ArrayList
Best Practices:
• Always use Generics (e.g., ArrayList<String>) for type safety.
• Choose meaningful variable names to improve code readability.
• Use enhanced for loops when the index is not needed.
• Test edge cases (e.g., empty lists, duplicates) for robust solutions.
Topic : Conclusion
Summary:
• The Java Collections Framework offers dynamic and flexible data structures.
• ArrayList is a powerful, resizable array implementation that supports a wide range
of operations.
• Mastering ArrayList operations is crucial for effective Java programming.
• Continue practicing and experimenting with ArrayList methods to build robust
applications.
Final Tips:
• Explore other collections (e.g., HashSet, LinkedList) to broaden your toolkit.
• Use these examples as a foundation for more complex data structures.
• Regular practice and code modifications will deepen your understanding.
THANK - YOU