0% found this document useful (0 votes)
3 views6 pages

L45Java Collections

Java Collections offer a framework for managing groups of objects, with key types including List, Set, Map, and Queue. Lists, which can contain duplicates, are primarily implemented through ArrayList and LinkedList, with generic collections providing type safety. Important operations include adding, removing, and checking for elements, along with methods for sorting and clearing collections.

Uploaded by

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

L45Java Collections

Java Collections offer a framework for managing groups of objects, with key types including List, Set, Map, and Queue. Lists, which can contain duplicates, are primarily implemented through ArrayList and LinkedList, with generic collections providing type safety. Important operations include adding, removing, and checking for elements, along with methods for sorting and clearing collections.

Uploaded by

licovo8928
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

L45:Java Collections

Introduction to Collections
Collections in Java provide a framework for storing and manipulating groups of
objects. They are similar to data structures in other programming languages but
come with built-in implementations that make it easier to manage collections of
data.

Key Concepts of Collections


Generic Collections: Collections that specify the type of objects they
contain.

Non-Generic Collections: Collections that can contain objects of any type.

Memory Management
ch
.te
Java memory is divided into three sections:
de

1. Stack: Stores method calls and local variables. Fixed size, non-expandable.
co

2. Heap: Stores objects and dynamic memory allocation. Expandable and


deletable.
n2

3. Code Section: Stores compiled code, like print statements.


ar

Types of Collections
le

Java collections are categorized into four main types:

1. List

2. Set

3. Map

4. Queue

Focus on List Collections

List Overview
A List in Java is an ordered collection (also known as a sequence). Lists can
contain duplicate elements. The main implementations of the List interface are:

L45:Java Collections 1
1. ArrayList

2. LinkedList

ArrayList
An ArrayList is a resizable array, which grows dynamically as more elements
are added.
Key Operations:

1. Create an ArrayList:

ArrayList<Integer> list = new ArrayList<>();

2. Add Elements:

ch
[Link](10);
[Link](20);
.te
3. Print Elements:
de

for (Integer num : list) {


co

[Link](num);
n2

}
ar

4. Non-Generic ArrayList:
le

ArrayList list = new ArrayList();


[Link]("Hello");
[Link](10);
[Link](3.14);

5. Generic ArrayList:

ArrayList<String> list = new ArrayList<>();


[Link]("Hello");

LinkedList

L45:Java Collections 2
A LinkedList is a linear data structure where each element is a separate object.
Each element (node) contains a reference to the next node and optionally the
previous node.

Key Operations:

1. Create a LinkedList:

LinkedList<Integer> list = new LinkedList<>();

2. Add Elements:

[Link](10);
[Link](20);

3. Print Elements:

for (Integer num : list) {


ch
.te
[Link](num);
de

}
co

Generic vs Non-Generic Collections


n2

Non-Generic: Can store any type of object.


ar

ArrayList list = new ArrayList();


le

[Link]("Hello");
[Link](10);

Generic: Restricts the type of objects it can store.

ArrayList<String> list = new ArrayList<>();


[Link]("Hello");

Important Methods in ArrayList


1. Adding Elements:

[Link]("Element");

L45:Java Collections 3
2. Removing Elements:

[Link](2); // Removes element at index 2

3. Cloning a List:

ArrayList<Integer> newList = (ArrayList<Integer>) list.c


lone();

4. Clearing a List:

[Link]();

5. Checking for an Element:

ch
boolean exists = [Link]("Element");
.te
6. Getting Size of List:
de

int size = [Link]();


co
n2

7. Sorting a List:
ar

[Link](list);
le

8. Replace All Elements:

[Link](e -> e * 2);

Hands-On Example
Example 1: Create and Manipulate an ArrayList

import [Link];

public class Main {


public static void main(String[] args) {
ArrayList<Integer> list = new ArrayList<>();

L45:Java Collections 4
[Link](10);
[Link](20);
[Link](30);

[Link]("List: " + list);

[Link](1);
[Link]("After removal: " + list);

[Link](1, 25);
[Link]("After insertion: " + list);

boolean contains = [Link](25);


[Link]("Contains 25: " + contains);

ch
[Link]();
.te
[Link]("After clearing: " + list);
}
de

}
co

Example 2: Using LinkedList


n2

import [Link];
ar

public class Main {


le

public static void main(String[] args) {


LinkedList<String> list = new LinkedList<>();
[Link]("A");
[Link]("B");
[Link]("C");

[Link]("List: " + list);

[Link]("X");
[Link]("Y");
[Link]("After additions: " + list);

[Link]();

L45:Java Collections 5
[Link]();
[Link]("After removals: " + list);
}
}

Summary
Collections in Java provide a powerful framework to manage groups of
objects.

Lists are ordered collections that can contain duplicate elements.

ArrayList and LinkedList are the two main implementations of the List
interface.

Generic collections provide type safety, while non-generic collections are


more flexible but can be harder to manage.

ch
Key methods of collections include add, remove, contains, size, clear, and
.te
sort.
de

By understanding these concepts and practicing with the examples provided,


students can effectively utilize Java collections to manage data in their
co

applications.
n2
ar
le

L45:Java Collections 6

You might also like