JAVA ASSIGNMENT
COLLECTIONS - II
NAME : Vetriselvan K
ROLL NO : 2024242018
BRANCH: [Link] integrated
(Information Technology)
1.
import [Link];
import [Link];
public class qs1{
public static void main(String[] args)
{
Map<String,Integer>sample = new HashMap<>();
[Link]("Vetriselvan",2018);
[Link]("Adhi",2022);
[Link]("Thiru",2010);
[Link]((key, Value) -> {
[Link]("Name : " + key + "\nRoll no : " + Value);
});
}
}
2.
import [Link];
import [Link];
public class qs2{
public static void main(String[] args)
{
Map<Integer,String> search = new HashMap<>();
[Link](1,"Apple");
[Link](2,"Banana");
[Link](54,"Guva");
int searchkey = 2;
if([Link](searchkey))
{
[Link](searchkey + " Exists" + "\nValue = " +
[Link](searchkey));
}
else
{
[Link](searchkey + "Notfound");
}
if()
{
[Link]("Key 400 is missing");
}
}
}
3.
import [Link];
import [Link];
class q3{
public static void main(String[] args)
{
String input = "hello all welcome to the internet world Hello world ";
String[] words = [Link]().split("\\s+");
Map<String, Integer> wc = new HashMap<>();
for(String word : words)
{
[Link](word,[Link](word,0)+1);
}
[Link]("Word occurance");
[Link]((k , v) -> [Link](k + ":" + v));
}
}
4.
import [Link];
import [Link];
public class qs4{
public static void main(String[] args)
{
Map<String,Integer>sample = new HashMap<>();
[Link]("Vetriselvan",2018);
[Link]("Adhi",2022);
[Link]("Thiru",2010);
Map<String,Integer> destination = new HashMap(sample);
[Link]((key, Value) -> {
[Link]("Name : " + key + "\nRoll no : " + Value);
});
[Link]("Copied = " + destination);
}
}
5.
import [Link].*;
public class DuplicateCharacters {
public static void main(String[] args) {
String str = "programming";
HashMap<Character, Integer> map = new HashMap<>();
for (char ch : [Link]()) {
[Link](ch, [Link](ch, 0) + 1);
}
6.
import [Link];
import [Link];
import [Link];
public class qs6{
public static void main(String[] args)
{
Map<String,Integer>sample = new HashMap<>();
[Link]("Vetriselvan",2018);
[Link]("Adhi",2022);
[Link]("Thiru",2010);
Map<String , Integer> sorted = new TreeMap<>(sample);
[Link]("HashMap (Unsorted) = " + sample);
[Link]("TreeMap (Sorted) = " + sorted);
}
}
7.
import [Link].*;
public class SortHashSet {
public static void main(String[] args) {
HashSet<Integer> set = new HashSet<>();
[Link](30);
[Link](10);
[Link](20);
List<Integer> list = new ArrayList<>(set);
[Link](list);
[Link](list);
}
}
8.
import [Link].*;
public class ArrayToHashSet {
public static void main(String[] args) {
Integer[] arr = {1, 2, 3, 4, 2};
HashSet<Integer> set = new HashSet<>([Link](arr));
[Link](set);
}
}
9.
import [Link].*;
public class StackExample {
public static void main(String[] args) {
Stack<Integer> stack = new Stack<>();
[Link](10);
[Link](20);
[Link](30);
[Link](stack);
}
}
10.
import [Link];
class CircularQueue {
int[] arr;
int front, rear, size, capacity;
CircularQueue(int capacity) {
[Link] = capacity;
arr = new int[capacity];
front = size = 0;
rear = capacity - 1;
}
boolean isFull() {
return size == capacity;
}
boolean isEmpty() {
return size == 0;
}
void enqueue(int item) {
if (isFull()) return;
rear = (rear + 1) % capacity;
arr[rear] = item;
size++;
}
int dequeue() {
if (isEmpty()) return -1;
int item = arr[front];
front = (front + 1) % capacity;
size--;
return item;
}
}
11.
import [Link].*;
public class ListToQueue {
public static void main(String[] args) {
List<Integer> list = [Link](1, 2, 3, 4);
Queue<Integer> queue = new LinkedList<>(list);
[Link](queue);
}
}
12.
import [Link].*;
public class WordFrequencyTreeMap {
public static void main(String[] args) {
String text = "apple banana apple orange banana apple";
String[] words = [Link](" ");
TreeMap<String, Integer> map = new TreeMap<>();
for (String word : words) {
[Link](word, [Link](word, 0) + 1);
}
[Link](map);
}
}
13.
import [Link].*;
public class PriorityQueueExample {
public static void main(String[] args) {
PriorityQueue<Integer> pq = new PriorityQueue<>();
[Link](30);
[Link](10);
[Link](20);
while (![Link]()) {
[Link]([Link]());
}
}
}
14.
import [Link].*;
public class StackUsingArrayDeque {
public static void main(String[] args) {
ArrayDeque<Integer> stack = new ArrayDeque<>();
[Link](10);
[Link](20);
[Link](30);
while (![Link]()) {
[Link]([Link]());
}
}
}
15.
import [Link].*;
public class QueueUsingArrayDeque {
public static void main(String[] args) {
ArrayDeque<Integer> queue = new ArrayDeque<>();
[Link](10);
[Link](20);
[Link](30);
while (![Link]()) {
[Link]([Link]());
}
}
}