0% found this document useful (0 votes)
15 views18 pages

Collections

The document provides an overview of collections in Java, detailing their importance in storing and managing data efficiently through the Java Collections Framework (JCF). It describes various types of collections such as List, Set, Map, and Queue, along with their interfaces and implementations. Additionally, it discusses the characteristics of ArrayList and Vector, highlighting their differences in terms of synchronization and performance in multithreading environments.

Uploaded by

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

Collections

The document provides an overview of collections in Java, detailing their importance in storing and managing data efficiently through the Java Collections Framework (JCF). It describes various types of collections such as List, Set, Map, and Queue, along with their interfaces and implementations. Additionally, it discusses the characteristics of ArrayList and Vector, highlighting their differences in terms of synchronization and performance in multithreading environments.

Uploaded by

Sheryl Arulini
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 18

Collections in Java

Collections

• Collections are fundamental data structures that allow you to


store, manipulate, and organize groups of related objects.
• They provide a way to manage data efficiently and flexibly.
• In Java, collections are part of the Java Collections Framework
(JCF), which is a set of interfaces and classes for handling
various types of data structures.
Why Use Collections?

Imagine you need to store a list of names, manage a set


of unique elements, or maintain a mapping between
keys and values. Collections provide ready-made
solutions for these scenarios.

Collections offer better memory management, dynamic


resizing, and optimized algorithms for common
operations.
Types of Collections:
The JCF provides several interfaces and classes for
different collection types. Here are some common
ones:
• List: An ordered collection that allows duplicate
elements (e.g., ArrayList, LinkedList).
• Set: A collection that contains unique elements
(e.g., HashSet, TreeSet).
• Map: A key-value pair collection (e.g., HashMap,
TreeMap).
• Queue: A collection for managing elements in a
specific order (e.g., PriorityQueue, LinkedList).
Interfaces vs. Implementations:

JCF defines interfaces (e.g., List, Set, Map) that specify


the behavior of collections.

Implementations (e.g., ArrayList, HashSet, HashMap)


provide concrete implementations of these interfaces.
1. List (Ordered Collection):
import java.util.ArrayList;
import java.util.List;

class Lsteg{
public static void main(String[] args) {
List<String> names = new ArrayList<>();
names.add("Alice");
names.add("Bob");
names.add("Charlie");
System.out.println("Names: " + names);
System.out.println("Second name: " +
names.get(1));}}
import java.util.*;
public class colren {
public static void main(String[] args) {
Stack<Integer> stack = new Stack<>();
stack.push(1);
stack.push(2);
stack.push(3);
System.out.println("The popped element of the stack is: "
+ stack.pop());
System.out.println("Now the top element of the stack is:
" + stack.peek());
}}
import java.util.LinkedList; System.out.println("The list
elements are: "+ll);
public class colren {
ll.removeFirst();
public static void
main(String[] args) { ll.removeLast();
LinkedList<Integer> ll System.out.println("The list
= new LinkedList<>(); elements are: "+ll);
ll.add(1); ll.add(4,20);
ll.add(2); System.out.println("The list
elements are: "+ll);
ll.add(3);
}}
ll.add(4);
ll.add(2);
ll.add(6);
2. Set (Unique Elements)
import java.util.HashSet;
import java.util.Set;

class Seteg {
public static void main(String[] args) {
Set<Integer> numbers = new HashSet<>();
numbers.add(10);
numbers.add(20);
numbers.add(10); // Duplicate value (ignored)
System.out.println("Numbers: " + numbers);
}}
3. Map (Key-Value Pairs):
import java.util.HashMap;
import java.util.Map;
class MapExample {
public static void main(String[] args) {
Map<String, Integer> scores = new HashMap<>();
scores.put("Alice", 90);
scores.put("Bob", 85);
scores.put("Charlie", 95);

System.out.println("Bob's score: " +


scores.get("Bob"));
import java.util.*;
public class colren {
public static void main(String[] args) {
HashMap<Integer,Character> hm = new HashMap<>();
hm.put(1, 'a');
hm.put(2, 'b');
hm.put(3, 'c');
System.out.println("The key-value pairs are: " + hm);
}}
ArrayList

• ArrayList is not synchronized, so multiple threads can modify


it concurrently.
• ArrayList is faster since it is non-synchronized.
• This can lead to data corruption (e.g., lost updates,
inconsistent state).
• When the number of elements exceeds the capacity,
ArrayList increments its capacity by 50% of the current array
size.
ArrayList

import java.util.*;
public class colren {
public static void main(String[] args) {
ArrayList<String> al = new ArrayList<>();
al.add("Hello");
al.add("Everyone");
for(int i=0;i<al.size();i++) {
System.out.println(al.get(i));}
}}
Using Iterator
import java.util.*;
public class colren {
public static void main(String[] args) {
ArrayList<String> al = new ArrayList<>();
al.add("Hello");
al.add("Everyone");
Iterator it = al.iterator();
while(it.hasNext()) {
System.out.println(it.next());
}
}}
Here’s 1 eg where ArrayList is not suitable Thread thread1 = new
Thread(addTask);
Thread thread2 = new
Thread(addTask);
import java.util.ArrayList; thread1.start();
import java.util.List; thread2.start();

public class ArrayListExample { try {


public static void thread1.join();
main(String[] args) { thread2.join();
List<Integer> sharedList = new } catch (InterruptedException
ArrayList<>(); e) {
e.printStackTrace();
Runnable addTask = () -> {
}
for (int i = 0; i < 1000; i++) {
sharedList.add(i); System.out.println("ArrayList
} }; size: " + sharedList.size());
}
Vector
• Vector is synchronized which means only one thread can
access its methods at a time.
• It ensures thread safety.
• Even if multiple threads access it simultaneously, Vector
guarantees that only one thread can modify it at a time.
• Vector is slower due to synchronization.
• In a multithreading environment, it holds other threads in
a runnable or non-runnable state until the current thread
releases the lock of the object.
• Vector doubles its array size (increments by 100%)
Using Vector
Thread thread1 = new
import java.util.Vector; Thread(addTask);
Thread thread2 = new
public class Vve { Thread(addTask);
public static void main(String[]
args) { thread1.start();
Vector<Integer> sharedVector = new thread2.start();
Vector<>();
try {
Runnable addTask = () -> {
thread1.join();
for (int i = 0; i < 1000; i++)
thread2.join();
{
}catch (InterruptedException
sharedVector.add(i); e) {
} e.printStackTrace(); }
};
System.out.println("Vector size: "
+ sharedVector.size());

You might also like