0% found this document useful (0 votes)
9 views8 pages

Collection Framework Java Programs

The document provides complete Java programs demonstrating the Collection Framework, including List, Set, Queue, and Map, with examples for ArrayList, HashSet, PriorityQueue, and HashMap. Each program showcases common operations such as adding, removing, iterating, and searching elements. Additionally, it includes examples for LinkedList, Vector, Stack, Deque, and TreeSet, summarizing their features in a final table.

Uploaded by

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

Collection Framework Java Programs

The document provides complete Java programs demonstrating the Collection Framework, including List, Set, Queue, and Map, with examples for ArrayList, HashSet, PriorityQueue, and HashMap. Each program showcases common operations such as adding, removing, iterating, and searching elements. Additionally, it includes examples for LinkedList, Vector, Stack, Deque, and TreeSet, summarizing their features in a final table.

Uploaded by

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

complete Java programs for each part of the Collection Framework:

1️⃣ List
2️⃣ Set
3️⃣ Queue
4️⃣ Map

All programs are executable and show common operations like add, remove, iterate, search.

1️⃣ LIST Example Program (ArrayList)


List allows:

 duplicate values
 maintains insertion order
 index based access

import [Link].*;

public class ListExample {

public static void main(String[] args) {

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

// adding elements
[Link]("Java");
[Link]("Python");
[Link]("C++");
[Link]("Java"); // duplicate allowed

// display
[Link]("List elements: " + list);

// access by index
[Link]("Element at index 1: " + [Link](1));

// remove element
[Link]("Python");

// size
[Link]("Size: " + [Link]());

// iteration
[Link]("Using loop:");
for(String lang : list)
{
[Link](lang);
}

// check element
[Link]("Contains Java? " + [Link]("Java"));
}
}

2️⃣ SET Example Program (HashSet)


Set:

 does NOT allow duplicate values


 does NOT maintain order

import [Link].*;

public class SetExample {

public static void main(String[] args) {

Set<Integer> set = new HashSet<>();

// add elements
[Link](10);
[Link](20);
[Link](30);
[Link](10); // duplicate ignored

// display
[Link]("Set elements: " + set);

// remove
[Link](20);

// size
[Link]("Size: " + [Link]());

// iteration
for(int num : set)
{
[Link](num);
}

// check element
[Link]("Contains 30? " + [Link](30));
}
}

3️⃣ QUEUE Example Program


(PriorityQueue)
Queue follows:
FIFO (First In First Out)

import [Link].*;
public class QueueExample {

public static void main(String[] args) {

Queue<String> queue = new PriorityQueue<>();

// add elements
[Link]("A");
[Link]("B");
[Link]("C");

[Link]("Queue: " + queue);

// view first element


[Link]("Peek: " + [Link]());

// remove element
[Link]("Removed: " + [Link]());

[Link]("Queue after removal: " + queue);

// iteration
for(String item : queue)
{
[Link](item);
}
}
}

4️⃣ MAP Example Program (HashMap)


Map stores:
key → value pair

Key must be unique.

import [Link].*;

public class MapExample {

public static void main(String[] args) {

Map<Integer,String> map = new HashMap<>();

// add elements
[Link](1,"Java");
[Link](2,"Python");
[Link](3,"C++");

// display map
[Link]("Map: " + map);

// access value
[Link]("Value at key 2: " + [Link](2));

// remove element
[Link](3);
// size
[Link]("Size: " + [Link]());

// iterate keys
for(Integer key : [Link]())
{
[Link](key + " -> " + [Link](key));
}

// check key
[Link]("Contains key 1? " + [Link](1));
}
}

LinkedList, Vector, Stack, Deque (ArrayDeque), PriorityQueue, HashSet, TreeSet

Each program shows add, remove, display, iterate operations.

1️⃣ LinkedList Program


LinkedList:

 maintains insertion order


 allows duplicates
 fast insertion/deletion

import [Link].*;

public class LinkedListExample {

public static void main(String[] args) {

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

[Link]("Java");
[Link]("Python");
[Link]("C++");
[Link]("HTML");
[Link]("SQL");

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

[Link]("Python");

[Link]("After remove: " + list);

for(String s : list)
{
[Link](s);
}
}
}
2️⃣ Vector Program
Vector:

 synchronized
 thread safe
 similar to ArrayList

import [Link].*;

public class VectorExample {

public static void main(String[] args) {

Vector<Integer> v = new Vector<>();

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

[Link]("Vector: " + v);

[Link](1);

for(int num : v)
{
[Link](num);
}

[Link]("Size: " + [Link]());


}
}

3️⃣ Stack Program


Stack:
LIFO (Last In First Out)

import [Link].*;

public class StackExample {

public static void main(String[] args) {

Stack<String> stack = new Stack<>();

[Link]("A");
[Link]("B");
[Link]("C");

[Link]("Stack: " + stack);

[Link]("Top element: " + [Link]());


[Link]();

[Link]("After pop: " + stack);

for(String s : stack)
{
[Link](s);
}
}
}

4️⃣ Deque Program (ArrayDeque)


Deque:
Double ended queue
Insertion and deletion from both sides

import [Link].*;

public class DequeExample {

public static void main(String[] args) {

Deque<String> dq = new ArrayDeque<>();

[Link]("A");
[Link]("B");
[Link]("C");

[Link]("Deque: " + dq);

[Link]();
[Link]();

[Link]("After remove: " + dq);

for(String s : dq)
{
[Link](s);
}
}
}

5️⃣ PriorityQueue Program


PriorityQueue:
elements sorted automatically

import [Link].*;

public class PriorityQueueExample {

public static void main(String[] args) {


PriorityQueue<Integer> pq = new PriorityQueue<>();

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

[Link]("PriorityQueue: " + pq);

[Link]("Peek: " + [Link]());

[Link]();

[Link]("After poll: " + pq);

for(int num : pq)


{
[Link](num);
}
}
}

6️⃣ HashSet Program


HashSet:

 no duplicates
 no order

import [Link].*;

public class HashSetExample {

public static void main(String[] args) {

HashSet<String> set = new HashSet<>();

[Link]("Java");
[Link]("Python");
[Link]("Java");

[Link]("HashSet: " + set);

[Link]("Python");

for(String s : set)
{
[Link](s);
}

[Link]("Contains Java? " + [Link]("Java"));


}
}
7️⃣ TreeSet Program
TreeSet:

 sorted order
 no duplicates

import [Link].*;

public class TreeSetExample {

public static void main(String[] args) {

TreeSet<Integer> ts = new TreeSet<>();

[Link](50);
[Link](10);
[Link](30);
[Link](10);

[Link]("TreeSet: " + ts);

[Link](30);

for(int num : ts)


{
[Link](num);
}

[Link]("First element: " + [Link]());


[Link]("Last element: " + [Link]());
}
}

Final Summary Table


Class Feature

LinkedList fast insertion/deletion

Vector synchronized

Stack LIFO

Deque insert/remove both ends

PriorityQueue sorted queue

HashSet unique elements

TreeSet sorted unique elements

You might also like