Introduction to Java
Collections Framework
and Collection interface.
Session No.: 12
Course Name: Java Programming
Course Code: R1UC304C
Instructor Name: Neha Kumari
Galgotias Student-Centered Active Learning
Ecosystem
Review of the key concepts of session No. 11
What do you mean by User-defined Exception Handling?
Different ways to handle Exceptions?
Galgotias Student-Centered Active Learning
Ecosystem
Opening Questions
What is the purpose of using Arrays and kindly tell its
limitations.
What happens if you try to add more elements than the
declared size of an array?
Galgotias Student-Centered Active Learning
Ecosystem
At the end of this session students will be able to
Learning Outcome 1:
Explain the importance of Collection Framework.
Learning Outcome 2:
Identify Collection Interface hierarchy and understand its
basic functions.
Galgotias Student-Centered Active Learning
Ecosystem
1. Introduction
2. Arrays vs Collection
Session 3. Activity 1 - Think-Pair-Share
4. Collection Hierarchy
Outline 5. Basic functions
6. Activity 2 - Problem-Based Learning
7. Conclusion and Discussion
Galgotias Student-Centered Active Learning
Ecosystem
Introduction
• A collection — sometimes called a container — is simply an object
that groups multiple elements into a single unit.
• Collections are used to store, retrieve, manipulate, and
communicate aggregate data.
• A collection represents a group of objects, known as its elements.
Some collections allow duplicate elements and others do not.
Some are ordered and others unordered.
Galgotias Student-Centered Active Learning
Ecosystem
Arrays vs Collection
Array vs Collection
Collection Hierarchy
Activity 1: Reinforce decision-making in real-
time.
1. Why Use Collection Instead of Arrays?
2. Show different situations on Wooclap and ask:
"Which one would you use?“
• A list of months in a year
• A shopping cart in an app
• Storing student roll numbers
• A playlist of songs
Galgotias University
Java Collection basic functions
• add(E element): Adds a single element to the collection.
• addAll(Collection<? extends E> c): Adds all elements from another collection.
• remove(Object o): Removes a specific element from the collection.
• removeAll(Collection<?> c): Removes all elements that are also present in another collection.
• clear(): Removes all elements from the collection.
• size(): Returns the number of elements in the collection.
• get(int index): Returns the element at the specified position.
Galgotias University
Add an Element
import [Link];
public class Main {
public static void main(String[] args) {
ArrayList<String> cars = new ArrayList<String>();
[Link]("Volvo");
[Link]("BMW");
[Link]("Ford");
[Link]("Mazda");
[Link](cars);
}
}
Remove an Element by Index
public class Main {
public static void main(String[] args) {
ArrayList<String> colors = new ArrayList<>();
[Link]("Red");
[Link]("Green");
[Link]("Blue");
// Removing the element at index 1 (Green)
[Link](1);
[Link](colors); // Output:?
}
}
Galgotias University
Retrieve an Element
3. get(index)
public class Main {
public static void main(String[] args) {
ArrayList<String> colors = new ArrayList<>();
[Link]("Red");
[Link]("Green");
[Link]("Blue");
// Get the element at index 2
String color = [Link](2);
[Link](color); // Output: ?
}
}
Galgotias University
Activity: 2 Live Coding Challenge –
Find the Output!
1. ArrayList<Integer> numbers = new ArrayList<>();
[Link](10);
[Link](20);
[Link](30);
[Link](1);
[Link]([Link](1));
Q: What will be printed?
Galgotias University
Activity: 2 Live Coding Challenge –
Find the Output!
Q2: What is the output of this code?
import [Link];
public class Test {
public static void main(String[] args) {
ArrayList<Integer> numbers = new ArrayList<>();
[Link](5);
[Link](10);
[Link](15);
[Link]([Link](3));
}
} Galgotias University
Summary
• A collections framework is a unified architecture for representing and
manipulating collections.
• Collection Interfaces: These are abstract data types that represent
collections. Interfaces allow collections to be manipulated
independently of the details of their representation.
Galgotias Student-Centered Active Learning
Ecosystem
Attainment of LOs in
alignment to the learning
At the end of this session students will be able to
Attainment 1:
Understood the importance of Collection framework
Attainment 2:
Understood the Collection Hierarchy and basic functions.
Galgotias Student-Centered Active Learning
Ecosystem
Discussion on the post
session activities
1. Think about a shopping cart in an online store. How does the system
keep track of the items you add and remove?
2. How would you store a list of students in a class if the number of students
keeps changing dynamically?
3. If you need to store an unknown number of elements in Java, what data
structure would you choose? Why?
Galgotias Student-Centered Active Learning
Ecosystem
Next Session Topic
List interface and implementations: ArrayList,
LinkedList, Vector, Stack
Galgotias Student-Centered Active Learning
Ecosystem
Review and Reflection of
the students
Galgotias Student-Centered Active Learning
Ecosystem