1.
List all elements of an ArrayList Create an ArrayList of integers, add 5 elements, and print
all elements using a simple for loop.
package Assignment04;
import [Link];
import [Link];
public class ArrayListExample {
public static void main(String[] args) {
ArrayList<Integer> numbers = new ArrayList<>();
Scanner scanner = new Scanner([Link]);
[Link]("Enter 5 integers:");
for (int i = 0; i < 5; i++) {
[Link]("Enter number " + (i + 1) + ": ");
int num = [Link]();
[Link](num);
[Link]("Elements in the ArrayList:");
for (int i = 0; i < [Link](); i++) {
[Link]([Link](i));
[Link]();
Output:
2. Remove duplicates from a List
Given a List of strings with duplicates, use a HashSet to remove duplicates and print the
unique elements.
package Assignment04;
import [Link];
import [Link];
import [Link];
import [Link];
public class RemoveDuplicates {
public static void main(String[] args) {
List<String> names = [Link]("Ali", "Baba", "Ali", "Chalees", "Baba");
Set<String> uniqueNames = new HashSet<>(names);
[Link](uniqueNames);
}
}
Output:
3. Find maximum and minimum in a LinkedList
Add 10 random numbers to a LinkedList and find the maximum and minimum values
without sorting.
package Assignment04;
import [Link].*;
public class LinkedListMinMax {
public static void main(String[] args) {
LinkedList<Integer> numbers = new LinkedList<>();
Random rand = new Random();
for (int i = 0; i < 10; i++) {
[Link]([Link](100));
[Link]("List: " + numbers);
[Link]("Max: " + [Link](numbers));
[Link]("Min: " + [Link](numbers));
Output:
4. Count frequency of characters using HashMap
Given a string, count the frequency of each character using a HashMap and print the
results.
package Assignment04;
import [Link].*;
public class CharFrequency {
public static void main(String[] args) {
String input = "programming";
Map<Character, Integer> freq = new HashMap<>();
for (char ch : [Link]()) {
[Link](ch, [Link](ch, 0) + 1);
[Link](freq);
}
Output:
5. Sort a list of custom objects using ComparableCreate a Student class with id, name,
and age. Use an ArrayList of Student objects and sort them by age using the
Comparable interface.
package Assignment04;
import [Link].*;
class Student implements Comparable<Student> {
int id;
String name;
int age;
Student(int id, String name, int age) {
[Link] = id;
[Link] = name;
[Link] = age;
}
public int compareTo(Student s) {
return [Link] - [Link];
}
public String toString() {
return id + " " + name + " " + age;
}
}
public class SortStudents {
public static void main(String[] args) {
List<Student> students = new ArrayList<>();
[Link](new Student(1, "Poonam", 22));
[Link](new Student(2, "Divya", 20));
[Link](new Student(3, "Alizeh", 23));
[Link](students);
for (Student s : students) {
[Link](s);
}
}
}
Output:
6. Merge two sets and find intersection
Create two HashSet<Integer> with some common and some distinct elements. Write
methods to merge both sets and find their intersection.
package Assignment04;
import [Link].*;
public class SetMergeIntersection {
public static void main(String[] args) {
Set<Integer> set1 = new HashSet<>([Link](1, 2, 3, 4, 5));
Set<Integer> set2 = new HashSet<>([Link](4, 5, 6, 7, 8));
Set<Integer> merged = new HashSet<>(set1);
[Link](set2);
[Link]("Merged: " + merged);
Set<Integer> intersection = new HashSet<>(set1);
[Link](set2);
[Link]("Intersection: " + intersection);
}
}
Output:
7. Use TreeMap to store and display students by grade
Create a TreeMap<Integer, String> where key is the student's grade and value is student
name. Insert data and display in ascending order of grade.
package Assignment04;
import [Link].*;
public class TreeMapExample {
public static void main(String[] args) {
TreeMap<Integer, String> students = new TreeMap<>();
[Link](90, "Simmi");
[Link](85, "Kavya");
[Link](96, "Alex");
for ([Link]<Integer, String> entry : [Link]()) {
[Link]("Grade: " + [Link]() + ", Name: " + [Link]());
}
}
}
Output:
8. Implement a simple stack using LinkedList Use LinkedList to implement stack operations:
push, pop,peek. Test with integer values.
package Assignment04;
import [Link].*;
public class StackUsingLinkedList {
public static void main(String[] args) {
LinkedList<Integer> stack = new LinkedList<>();
[Link](10);
[Link](20);
[Link](30);
[Link](40);
[Link](60);
[Link]("Popped: " + [Link]());
[Link]("Peek: " + [Link]());
}
}
Output:
9. Implement LRU Cache using LinkedHashMap Create a class LRUCache with a fixed
capacity. Use LinkedHashMap to store key-value pairs and implement get put methods.
When cache exceeds capacity, remove least recently used entry
package Assignment04;
import [Link].*;
class LRUCache<K, V> extends LinkedHashMap<K, V> {
private int capacity;
public LRUCache(int capacity) {
super(capacity, 0.75f, true);
[Link] = capacity;
}
protected boolean removeEldestEntry([Link]<K, V> eldest) {
return size() > capacity;
}
}
public class LRUExample {
public static void main(String[] args) {
LRUCache<Integer, String> cache = new LRUCache<>(3);
[Link](1, "A");
[Link](2, "B");
[Link](3, "C");
[Link](cache);
[Link](1);
[Link](4, "D");
[Link](cache);
}
}
Output:
10. Multi-threaded producer-consumer problem using BinckingQueue:
Create a producer thread that adds elements to a LinkedBlockingQueue and a
consumer thread that consumes elements. Implement proper synchronization and
thread-safe collection use
package Assignment04;
import [Link].*;
class Producer implements Runnable {
private BlockingQueue<Integer> queue;
public Producer(BlockingQueue<Integer> queue) {
[Link] = queue;
public void run() {
try {
for (int count = 0; count < 7; count++) {
[Link]("Producing: " + count);
[Link](count);
[Link](500);
} catch (InterruptedException e) {
[Link]().interrupt();
}
}
}
class Consumer implements Runnable {
private BlockingQueue<Integer> queue;
public Consumer(BlockingQueue<Integer> queue) {
[Link] = queue;
public void run() {
try {
for (int i = 0; i < 7; i++) {
Integer value = [Link]();
[Link]("Consumed: " + value);
[Link](1000);
} catch (InterruptedException e) {
[Link]().interrupt();
}
}
public class ProducerConsumer {
public static void main(String[] args) {
BlockingQueue<Integer> queue = new LinkedBlockingQueue<>(5);
Thread producerThread = new Thread(new Producer(queue));
Thread consumerThread = new Thread(new Consumer(queue));
[Link]();
[Link]();
try {
[Link]();
[Link]();
} catch (InterruptedException e) {
[Link]().interrupt();
[Link]("Both Producer and Consumer have completed processing 7 items.");
}
}
Output: