What is Framework?
- A framework is a pre-written set of classes, libraries, and rules that help developers build applications faster.
Why Collection Framework?
- Java arrays are used to store multiple values of the same data type in a single variable. However, arrays have several limitations, which is why the Java Collection Framework was introduced.
Disadvantages of Array :
Fixed Size (Static Memory Allocation) :
- The size of an array must be defined when it is created, and it cannot be changed later.
Arrays Store Only Similar Data Types(Homowgeneous) :
- An array can store only one type of data.
No Built-in Methods :
- Arrays do not provide built-in functions for common operations.
For example:
Sorting
Searching
Adding elements
Removing elements
You must write extra code manually.
Memory Wastage
- If you declare a large array but use only a few elements, unused memory is wasted.
Advantages of Collection :
Dynamic Size (Resizable):
- Collections can grow or shrink automatically as elements are added or removed.
Built-in Methods:
- Collections provide many built-in methods that simplify programming.
Reduces Programming Effort :
- Because collections provide ready-made functionality. Less code,Faster development, and Easier maintenance.
ArrayList :
- ArrayList is one of the most commonly used classes in the Java Collections Framework.
- arraylist is a class that implements interface List.
- It is a class in java.util package.
- Default initial capacity of ArrayList is 10.
Features:
- Maintains insertion order.
- Allows duplicates.
- Dynamic size (can grow automatically).
- Can store null values.
Methods of ArrayList :
add() – Add elements :
ArrayList al = new ArrayList();
//Adds an element to the list
al.add("Pen");
al.add(10);
al.add("Pencil");
al.add(1);
System.out.println(al);
Output :
[Pen, 10, Pencil, 1]
//Add element at specific index
al.add(0,"NoteBook");
System.out.println(al);
Output :
[NoteBook, Pen, 10, Pencil, 1]
set() – Access element :
- Replace element at a specific index.
al.set(2,"Book");
System.out.println(al);
Output :
[NoteBook, Pen, Book, Pencil, 1]
get() – Access element :
Retrieve element using index.
System.out.println(al.get(0));
Output : NoteBook
remove() – Delete element :
- Remove by index :
al.remove(2);
System.out.println(al);
Output:
[NoteBook, Pen, Pencil, 1]
- Remove by object :
al.remove("Pen");
System.out.println(al);
Output:
[NoteBook, Pencil, 1]
size() – Get number of elements:
System.out.println(al.size());
Output : 3
contains() – Check element existence:
System.out.println(al.contains(1));
Output : true
indexOf() – Find element index :
System.out.println(al.indexOf("Pencil"));
output: true
clear() – Remove all elements :
isEmpty() – Check if list is empty:
al.clear();
System.out.println(al.isEmpty());
System.out.println(al);
output: []
What is HashSet?
- Hashet is a class that implements the set interface.
- It doesn't allow duplicate values.
- Objects are not sorted in insertion order.
- Only one null value is allowed.
Example :
HashSet hst = new HashSet();
hst.add("abi");
hst.add("baby");
hst.add("cat");
hst.add("cat");
hst.add(null);
hst.add(null);
System.out.println(hst);
Output:
[null, cat, baby, abi]


Top comments (0)