Collection (ASSIGNMENT)
Collection (ASSIGNMENT)
18SCSE1010646
Assignment(Collection)
Name: RITIK KUMAR
ArrayList Exercises:
1) Code:
import java.util.*;
list_Strings.add("Red");
list_Strings.add("Green");
list_Strings.add("Orange");
list_Strings.add("White");
list_Strings.add("Black");
System.out.println(list_Strings);
Output:
2) Code:
RITIK KUMAR. 18SCSE1010646
import java.util.*;
list_Strings.add("Red");
list_Strings.add("Green");
list_Strings.add("Orange");
list_Strings.add("White");
list_Strings.add("Black");
System.out.println(element);
Output:
3) Code:
import java.util.*;
list_Strings.add("Red");
list_Strings.add("Green");
list_Strings.add("Orange");
list_Strings.add("White");
list_Strings.add("Black");
System.out.println(list_Strings);
// Now insert a color at the first and last position of the list
list_Strings.add(0, "Pink");
list_Strings.add(5, "Yellow");
System.out.println(list_Strings);
Output:
4) Code:
import java.util.*;
list_Strings.add("Red");
list_Strings.add("Green");
list_Strings.add("Orange");
list_Strings.add("White");
list_Strings.add("Black");
System.out.println(list_Strings);
element = list_Strings.get(2);
Output:
5)Code:
import java.util.*;
list_Strings.add("Red");
list_Strings.add("Green");
list_Strings.add("Orange");
list_Strings.add("White");
list_Strings.add("Black");
System.out.println(list_Strings);
list_Strings.set(2, "Yellow");
System.out.println(list_Strings);
Output:
6) Code:
import java.util.*;
list_Strings.add("Red");
list_Strings.add("Green");
list_Strings.add("Orange");
RITIK KUMAR. 18SCSE1010646
list_Strings.add("White");
list_Strings.add("Black");
System.out.println(list_Strings);
list_Strings.remove(2);
Output:
7) Code:
import java.util.*;
list_Strings.add("Red");
list_Strings.add("Green");
list_Strings.add("Orange");
list_Strings.add("White");
list_Strings.add("Black");
if (list_Strings.contains("Red")) {
} else {
Output:
8) Code:
import java.util.*;
list_Strings.add("Red");
list_Strings.add("Green");
list_Strings.add("Orange");
list_Strings.add("White");
list_Strings.add("Black");
Collections.sort(list_Strings);
}
RITIK KUMAR. 18SCSE1010646
Output:
9) Code:
import java.util.*;
List1.add("1");
List1.add("2");
List1.add("3");
List1.add("4");
List2.add("A");
List2.add("B");
List2.add("C");
List2.add("D");
Collections.copy(List1, List2);
}
RITIK KUMAR. 18SCSE1010646
Output:
10)Code:
import java.util.*;
list_Strings.add("Red");
list_Strings.add("Green");
list_Strings.add("Orange");
list_Strings.add("White");
list_Strings.add("Black");
Collections.shuffle(list_Strings);
Output:
RITIK KUMAR. 18SCSE1010646
11) Code:
import java.util.*;
list_Strings.add("Red");
list_Strings.add("Green");
list_Strings.add("Orange");
list_Strings.add("White");
list_Strings.add("Black");
Collections.reverse(list_Strings);
Output:
12) Code:
import java.util.*;
RITIK KUMAR. 18SCSE1010646
list_Strings.add("Red");
list_Strings.add("Green");
list_Strings.add("Orange");
list_Strings.add("White");
list_Strings.add("Black");
Output:
13) Code:
import java.util.*;
c1.add("Red");
c1.add("Green");
c1.add("Black");
RITIK KUMAR. 18SCSE1010646
c1.add("White");
c1.add("Pink");
c2.add("Red");
c2.add("Green");
c2.add("Black");
c2.add("Pink");
System.out.println(c3);
Output:
14) Code:
import java.util.ArrayList;
import java.util.Collections;
c1.add("Red");
c1.add("Green");
c1.add("Black");
c1.add("White");
c1.add("Pink");
for(String a: c1){
System.out.println(a);
Collections.swap(c1, 0, 2);
for(String b: c1){
System.out.println(b);
Output:
RITIK KUMAR. 18SCSE1010646
15) Code:
import java.util.ArrayList;
import java.util.Collections;
c1.add("Red");
c1.add("Green");
c1.add("Black");
c1.add("White");
c1.add("Pink");
c2.add("Red");
c2.add("Green");
c2.add("Black");
c2.add("Pink");
RITIK KUMAR. 18SCSE1010646
a.addAll(c1);
a.addAll(c2);
Output:
16)Code:
import java.util.ArrayList;
import java.util.Collections;
c1.add("Red");
c1.add("Green");
c1.add("Black");
c1.add("White");
c1.add("Pink");
RITIK KUMAR. 18SCSE1010646
Output:
17) Code:
import java.util.ArrayList;
import java.util.Collections;
c1.add("Red");
c1.add("Green");
c1.add("Black");
c1.add("White");
c1.add("Pink");
c1.removeAll(c1);
}
RITIK KUMAR. 18SCSE1010646
Output:
18) Code:
import java.util.ArrayList;
import java.util.Collections;
c1.add("Red");
c1.add("Green");
c1.add("Black");
c1.add("White");
c1.add("Pink");
c1.removeAll(c1);
Output:
RITIK KUMAR. 18SCSE1010646
19) Code:
import java.util.ArrayList;
import java.util.Collections;
c1.add("Red");
c1.add("Green");
c1.add("Black");
c1.add("White");
c1.add("Pink");
c1.trimToSize();
System.out.println(c1);
Output:
20) Code:
import java.util.ArrayList;
import java.util.Collections;
RITIK KUMAR. 18SCSE1010646
c1.add("Red");
c1.add("Green");
c1.add("Black");
//Increase capacity to 6
c1.ensureCapacity(6);
c1.add("White");
c1.add("Pink");
c1.add("Yellow");
Output:
21) Code:
import java.util.ArrayList;
color.add("Red");
RITIK KUMAR. 18SCSE1010646
color.add("Green");
color.set(1,new_color);
int num=color.size();
for(int i=0;i<num;i++)
System.out.println(color.get(i));
Output:
22) Code:
import java.util.ArrayList;
c1.add("Red");
c1.add("Green");
c1.add("Black");
c1.add("White");
RITIK KUMAR. 18SCSE1010646
c1.add("Pink");
System.out.println(c1.get(index));
Output:
LinkedList Exercises
1) Code:
import java.util.LinkedList;
l_list.add("Red");
l_list.add("Green");
RITIK KUMAR. 18SCSE1010646
l_list.add("Black");
l_list.add("White");
l_list.add("Pink");
l_list.add("Yellow");
Output:
2) Code:
import java.util.LinkedList;
l_list.add("Red");
l_list.add("Green");
l_list.add("Black");
l_list.add("White");
l_list.add("Pink");
System.out.println(element);
Output:
3) Code:
import java.util.LinkedList;
import java.util.Iterator;
l_list.add("Red");
l_list.add("Green");
l_list.add("Black");
l_list.add("White");
l_list.add("Pink");
Iterator p = l_list.listIterator(1);
while (p.hasNext()) {
System.out.println(p.next());
Output:
4) Code:
import java.util.LinkedList;
import java.util.Iterator;
l_list.add("Red");
l_list.add("Green");
l_list.add("Black");
l_list.add("Pink");
l_list.add("orange");
Iterator it = l_list.descendingIterator();
while (it.hasNext()) {
System.out.println(it.next());
Output:
5) Code:
import java.util.LinkedList;
l_list.add("Red");
l_list.add("Green");
RITIK KUMAR. 18SCSE1010646
l_list.add("Black");
l_list.add("White");
l_list.add("Pink");
System.out.println("Let add the Yellow color after the Red Color: " + l_list);
l_list.add(1, "Yellow");
Output:
6)Code:
import java.util.LinkedList;
l_list.add("Red");
l_list.add("Green");
l_list.add("Black");
l_list.addFirst("White");
l_list.addLast("Pink");
Output:
7) Code:
import java.util.LinkedList;
l_list.add("Red");
l_list.add("Green");
l_list.add("Black");
l_list.offerFirst("Pink");
Output:
8) Code:
import java.util.LinkedList;
l_list.add("Red");
l_list.add("Green");
l_list.add("Black");
l_list.offerLast("Pink");
Output:
9) Code:
RITIK KUMAR. 18SCSE1010646
import java.util.LinkedList;
l_list.add("Red");
l_list.add("Green");
l_list.add("Black");
new_l_list.add("White");
new_l_list.add("Pink");
// Add the collection in the second position of the existing linked list
l_list.addAll(1, new_l_list);
System.out.println("LinkedList:" + l_list);
}
RITIK KUMAR. 18SCSE1010646
Output:
10) Code:
import java.util.LinkedList;
import java.util.Iterator;
l_list.add("Red");
l_list.add("Green");
l_list.add("Black");
l_list.add("Pink");
l_list.add("orange");
Output:
11) Code:
import java.util.LinkedList;
import java.util.Iterator;
l_list.add("Red");
l_list.add("Green");
l_list.add("Black");
l_list.add("Pink");
l_list.add("orange");
Output:
12) Code:
import java.util.*;
l_list.add("Red");
l_list.add("Green");
l_list.add("Black");
l_list.add("Pink");
l_list.add("orange");
// Remove the element in third position from the said linked list
l_list.remove(2);
Output:
13) Code:
import java.util.*;
l_list.add("Red");
l_list.add("Green");
l_list.add("Black");
l_list.add("Pink");
l_list.add("orange");
Output:
14) Code:
import java.util.*;
l_list.add("Red");
l_list.add("Green");
RITIK KUMAR. 18SCSE1010646
l_list.add("Black");
l_list.add("Pink");
l_list.add("orange");
l_list.clear();
Output:
15) Code:
import java.util.*;
l_list.add("Red");
RITIK KUMAR. 18SCSE1010646
l_list.add("Green");
l_list.add("Black");
l_list.add("Pink");
l_list.add("orange");
Collections.swap(l_list, 0, 2);
Output:
16) Code:
import java.util.*;
l_list.add("Red");
l_list.add("Green");
RITIK KUMAR. 18SCSE1010646
l_list.add("Black");
l_list.add("Pink");
l_list.add("orange");
Collections.shuffle(l_list);
Output:
17) Code:
import java.util.*;
c1.add("Red");
c1.add("Green");
c1.add("Black");
c1.add("White");
c1.add("Pink");
RITIK KUMAR. 18SCSE1010646
c2.add("Red");
c2.add("Green");
c2.add("Black");
c2.add("Pink");
a.addAll(c1);
a.addAll(c2);
Output:
18) Code:
import java.util.*;
c1.add("Red");
c1.add("Green");
c1.add("Black");
c1.add("White");
c1.add("Pink");
newc1 = (LinkedList)c1.clone();
Output:
19) Code:
import java.util.*;
c1.add("Red");
c1.add("Green");
c1.add("Black");
c1.add("White");
RITIK KUMAR. 18SCSE1010646
c1.add("Pink");
Output:
20) Code:
import java.util.*;
c1.add("Red");
c1.add("Green");
c1.add("Black");
c1.add("White");
c1.add("Pink");
// Retrieve but does not remove, the first element of a linked list
RITIK KUMAR. 18SCSE1010646
String x = c1.peekFirst();
Output:
21) Code:
import java.util.*;
c1.add("Red");
c1.add("Green");
c1.add("Black");
c1.add("White");
c1.add("Pink");
// Retrieve but does not remove, the last element of a linked list
String x = c1.peekLast();
Output:
22) Code:
import java.util.*;
c1.add("Red");
c1.add("Green");
c1.add("Black");
c1.add("White");
c1.add("Pink");
if (c1.contains("Green")) {
} else {
}
RITIK KUMAR. 18SCSE1010646
if (c1.contains("Orange")) {
} else {
Output:
23) Code:
import java.util.*;
linked_list.add("Red");
linked_list.add("Green");
linked_list.add("Black");
linked_list.add("White");
linked_list.add("Pink");
RITIK KUMAR. 18SCSE1010646
System.out.println(str);
Output:
24) Code:
import java.util.*;
c1.add("Red");
c1.add("Green");
c1.add("Black");
c1.add("White");
c1.add("Pink");
RITIK KUMAR. 18SCSE1010646
c2.add("Red");
c2.add("Green");
c2.add("Black");
c2.add("Orange");
System.out.println(c3);
Output:
25) Code:
import java.util.LinkedList;
import java.util.Collections;
c1.add("Red");
c1.add("Green");
c1.add("Black");
RITIK KUMAR. 18SCSE1010646
c1.add("White");
c1.add("Pink");
c1.removeAll(c1);
Output:
26) Code:
import java.util.LinkedList;
import java.util.Collections;
c1.add("Red");
c1.add("Green");
c1.add("Black");
c1.add("White");
c1.add("Pink");
c1.set(1, "Orange");
Output:
RITIK KUMAR.
18SCSE1010646
Java
Collection: HashSet Exercises
1.)Code:-
import java.util.HashSet;
public class Main {
public static void main(String[] args) {
2.)Code:-
import java.util.*;
import java.util.Iterator;
public class Main {
public static void main(String[] args) {
h_set.add("Red");
h_set.add("Green");
h_set.add("Black");
h_set.add("White");
h_set.add("Pink");
h_set.add("Yellow");
Iterator<String> p = h_set.iterator();
while (p.hasNext()) {
System.out.println(p.next());
}
}
}
RITIK KUMAR.
18SCSE1010646
Output:-
3.)Code:-
import java.util.*;
import java.util.Iterator;
public class Main {
public static void main(String[] args) {
h_set.add("Red");
h_set.add("Green");
h_set.add("Black");
h_set.add("White");
h_set.add("Pink");
h_set.add("Yellow");
System.out.println("Original Hash Set: " + h_set);
System.out.println("Size of the Hash Set: " + h_set.size());
}
}
RITIK KUMAR.
18SCSE1010646
Output:-
4.)Code:-
import java.util.*;
h_set.add("Red");
h_set.add("Green");
h_set.add("Black");
h_set.add("White");
h_set.add("Pink");
h_set.add("Yellow");
System.out.println("Original Hash Set: " + h_set);
h_set.removeAll(h_set);
RITIK KUMAR.
18SCSE1010646
5.)Code:-
import java.util.*;
h_set.add("Red");
h_set.add("Green");
h_set.add("Black");
h_set.add("White");
h_set.add("Pink");
h_set.add("Yellow");
System.out.println("Original Hash Set: " + h_set);
RITIK KUMAR.
18SCSE1010646
6.)Code:-
import java.util.*;
h_set.add("Red");
h_set.add("Green");
h_set.add("Black");
h_set.add("White");
RITIK KUMAR.
18SCSE1010646
h_set.add("Pink");
h_set.add("Yellow");
System.out.println("Original Hash Set: " + h_set);
HashSet <String> new_h_set = new HashSet <String> ();
new_h_set = (HashSet)h_set.clone();
System.out.println("Cloned Hash Set: " + new_h_set);
}
}
Output:-
7.)Code:-
import java.util.*;
public class Main {
public static void main(String[] args) {
h_set.add("Red");
h_set.add("Green");
h_set.add("Black");
RITIK KUMAR.
18SCSE1010646
h_set.add("White");
h_set.add("Pink");
h_set.add("Yellow");
System.out.println("Original Hash Set: " + h_set);
8.)Code:-
import java.util.*;
public class Main {
public static void main(String[] args) {
h_set.add("Red");
h_set.add("Green");
h_set.add("Black");
h_set.add("White");
h_set.add("Pink");
h_set.add("Yellow");
System.out.println("Original Hash Set: " + h_set);
9.)Code:-
import java.util.*;
h_set.add("Red");
h_set.add("Green");
h_set.add("Black");
h_set.add("White");
h_set.add("Pink");
h_set.add("Yellow");
System.out.println("Original Hash Set: " + h_set);
10.)code:-
import java.util.*;
public class Main {
RITIK KUMAR.
18SCSE1010646
h_set.add("Red");
h_set.add("Green");
h_set.add("Black");
h_set.add("White");
11.)code:-
import java.util.*;
RITIK KUMAR.
18SCSE1010646
h_set1.add("Red");
h_set1.add("Green");
h_set1.add("Black");
h_set1.add("White");
System.out.println("Frist HashSet content: "+h_set1);
HashSet<String>h_set2 = new HashSet<String>();
h_set2.add("Red");
h_set2.add("Pink");
h_set2.add("Black");
h_set2.add("Orange");
System.out.println("Second HashSet content: "+h_set2);
h_set1.retainAll(h_set2);
System.out.println("HashSet content:");
System.out.println(h_set1);
}
}
Output:-
12.)Code:-
import java.util.*;
RITIK KUMAR.
18SCSE1010646
h_set.add("Red");
h_set.add("Green");
h_set.add("Black");
h_set.add("White");
System.out.println("Original hash set contains: "+
h_set);
h_set.clear();
Code:-
import java.util.TreeSet;
public class Main{
public static void main(String[] args) {
TreeSet<String> tree_set = new TreeSet<String>();
tree_set.add("Red");
tree_set.add("Green");
tree_set.add("Orange");
tree_set.add("White");
tree_set.add("Black");
System.out.println("Tree set: ");
System.out.println(tree_set);
}
}
Output:-
Code:-
import java.util.TreeSet;
public class Main{
public static void main(String[] args) {
RITIK KUMAR.
18SCSE1010646
Code:-
import java.util.TreeSet;
public class Mian {
public static void main(String[] args) {
RITIK KUMAR.
18SCSE1010646
tree_set1.addAll(tree_set2);
System.out.println("Tree set1: "+tree_set1);
}
}
Output:-
Code:-
import java.util.TreeSet;
import java.util.Iterator;
public class Main {
RITIK KUMAR.
18SCSE1010646
t_set.add("Red");
t_set.add("Green");
t_set.add("Black");
t_set.add("Pink");
t_set.add("orange");
Code:-
import java.util.TreeSet;
public class Exercise5 {
public static void main(String[] args) {
RITIK KUMAR.
18SCSE1010646
Code:-
import java.util.TreeSet;
import java.util.Iterator;
RITIK KUMAR.
18SCSE1010646
t_set.add("Red");
t_set.add("Green");
t_set.add("Black");
t_set.add("Pink");
t_set.add("orange");
Code:-
import java.util.TreeSet;
import java.util.Iterator;
RITIK KUMAR.
18SCSE1010646
t_set.add("Red");
t_set.add("Green");
t_set.add("Black");
t_set.add("Pink");
t_set.add("orange");
System.out.println("Original tree set: " + t_set);
System.out.println("Size of the tree set: " + t_set.size());
}
}
Output:-
Code:-
import java.util.TreeSet;
import java.util.Iterator;
RITIK KUMAR.
18SCSE1010646
t_set1.add("Red");
t_set1.add("Green");
t_set1.add("Black");
t_set1.add("White");
System.out.println("Free Tree set: "+t_set1);
Code:-
import java.util.TreeSet;
import java.util.Iterator;
RITIK KUMAR.
18SCSE1010646
tree_num.add(1);
tree_num.add(2);
tree_num.add(3);
tree_num.add(5);
tree_num.add(6);
tree_num.add(7);
tree_num.add(8);
tree_num.add(9);
tree_num.add(10);
treeheadset = (TreeSet)tree_num.headSet(7);
Iterator iterator;
iterator = treeheadset.iterator();
Code:-
import java.util.TreeSet;
import java.util.Iterator;
RITIK KUMAR.
18SCSE1010646
tree_num.add(10);
tree_num.add(22);
tree_num.add(36);
tree_num.add(25);
tree_num.add(16);
tree_num.add(70);
tree_num.add(82);
tree_num.add(89);
tree_num.add(14);
Code:-
import java.util.TreeSet;
import java.util.Iterator;
public class Exercise11 {
RITIK KUMAR.
18SCSE1010646
tree_num.add(10);
tree_num.add(22);
tree_num.add(36);
tree_num.add(25);
tree_num.add(16);
tree_num.add(70);
tree_num.add(82);
tree_num.add(89);
tree_num.add(14);
Code:-
import java.util.TreeSet;
import java.util.Iterator;
RITIK KUMAR.
18SCSE1010646
tree_num.add(10);
tree_num.add(22);
tree_num.add(36);
tree_num.add(25);
tree_num.add(16);
tree_num.add(70);
tree_num.add(82);
tree_num.add(89);
tree_num.add(14);
Code:-
import java.util.TreeSet;
import java.util.Iterator;
tree_num.add(10);
tree_num.add(22);
tree_num.add(36);
tree_num.add(25);
tree_num.add(16);
tree_num.add(70);
tree_num.add(82);
tree_num.add(89);
tree_num.add(14);
Code:-
import java.util.TreeSet;
import java.util.Iterator;
tree_num.add(10);
tree_num.add(22);
tree_num.add(36);
tree_num.add(25);
tree_num.add(16);
tree_num.add(70);
tree_num.add(82);
tree_num.add(89);
tree_num.add(14);
System.out.println("Original tree set: "+tree_num);
System.out.println("Removes the first(lowest) element:
"+tree_num.pollFirst());
System.out.println("Tree set after removing first element:
"+tree_num);
}
}
Output:-
Code:-
import java.util.TreeSet;
import java.util.Iterator;
tree_num.add(10);
tree_num.add(22);
tree_num.add(36);
tree_num.add(25);
tree_num.add(16);
tree_num.add(70);
tree_num.add(82);
tree_num.add(89);
tree_num.add(14);
System.out.println("Original tree set: "+tree_num);
System.out.println("Removes the last element:
"+tree_num.pollLast());
System.out.println("Tree set after removing last element:
"+tree_num);
}
}
output:-
Code:-
import java.util.TreeSet;
import java.util.Iterator;
tree_num.add(10);
tree_num.add(22);
tree_num.add(36);
tree_num.add(25);
tree_num.add(16);
tree_num.add(70);
tree_num.add(82);
tree_num.add(89);
tree_num.add(14);
System.out.println("Original tree set: "+tree_num);
System.out.println("Removes 70 from the list:
"+tree_num.remove(70));
System.out.println("Tree set after removing last element:
"+tree_num);
}
}
Output:-
RITIK KUMAR.
18SCSE1010646
PriorityQueue Exercises
1. Write a Java program to create a new priority queue, add some colors (string) and print out
the elements of the priority queue.
Java Code:
import java.util.PriorityQueue;
public class Exercise1 {
public static void main(String[] args) {
PriorityQueue<String> queue=new PriorityQueue<String>();
queue.add("Red");
queue.add("Green");
queue.add("Orange");
queue.add("White");
queue.add("Black");
System.out.println("Elements of the Priority Queue: ");
System.out.println(queue);
}
}
Output:-
Java Code:
import java.util.PriorityQueue;
public class Exercise2 {
public static void main(String[] args) {
PriorityQueue<String> pq = new PriorityQueue<String>();
pq.add("Red");
pq.add("Green");
pq.add("Orange");
pq.add("White");
pq.add("Black");
System.out.println("Elements of the Priority Queue: ");
// iterate the Priority Queue
for (String element : pq) {
System.out.println(element);
}
}
}
Output:-
3. Write a Java program to add all the elements of a priority queue to another priority queue.
Java Code:
import java.util.PriorityQueue;
public class Exercise3 {
public static void main(String[] args) {
PriorityQueue<String> queue1 = new PriorityQueue<String>();
queue1.add("Red");
queue1.add("Green");
queue1.add("Orange");
System.out.println("Priority Queue1: "+queue1);
PriorityQueue<String> queue2 = new PriorityQueue<String>();
queue2.add("Pink");
queue2.add("White");
queue2.add("Black");
System.out.println("Priority Queue2: "+queue2);
// adding queue2 to queue1
queue1.addAll(queue2);
System.out.println("New Priority Queue1: "+queue1);
}
}
Output:-
Java Code:
import java.util.PriorityQueue;
5. Write a Java program to remove all the elements from a priority queue.
Java Code:
import java.util.*;
public class Example5 {
public static void main(String[] args) {
// Create Priority Queue
PriorityQueue<String> pq1 = new PriorityQueue<String>();
// use add() method to add values in the Priority Queue
pq1.add("Red");
pq1.add("Green");
pq1.add("Black");
pq1.add("White");
System.out.println("Original Priority Queue: "+pq1);
Java Code:
import java.util.PriorityQueue;
public class Exercise6 {
public static void main(String[] args) {
// create an empty Priority Queue
PriorityQueue<String> pq = new PriorityQueue<String>();
// use add() method to add values in the Priority Queue
pq.add("Red");
pq.add("Green");
pq.add("Black");
pq.add("Pink");
pq.add("orange");
System.out.println("Priority Queue: " + pq);
System.out.println("Size of the Priority Queue: " + pq.size());
}
}
Output:-
Java Code:
import java.util.PriorityQueue;
public class Exercise7 {
public static void main(String[] args) {
// Create a empty Priority Queue
PriorityQueue<String> pq1 = new PriorityQueue<String>();
// use add() method to add values in the Priority Queue
pq1.add("Red");
pq1.add("Green");
pq1.add("Black");
pq1.add("White");
System.out.println("First Priority Queue: "+pq1);
PriorityQueue<String> pq2 = new PriorityQueue<String>();
pq2.add("Red");
pq2.add("Pink");
pq2.add("Black");
pq2.add("Orange");
System.out.println("Second Priority Queue: "+pq2);
//comparison output in Priority Queue
for (String element : pq1){
System.out.println(pq2.contains(element) ? "Yes" : "No");
}
}
}
Output:-
RITIK KUMAR.
18SCSE1010646
8. Write a Java program to retrieve the first element of the priority queue.
Java Code:
import java.util.PriorityQueue;
public class Example8 {
public static void main(String[] args) {
Java Code:
import java.util.PriorityQueue;
Output:-
10. Write a Java program to convert a priority queue to an array containing all of the elements of the
queue.
Java Code:
import java.util.*;
}
}
Output:-
11. Write a Java program to convert a Priority Queue elements to a string representation.
Java Code:
import java.util.*;
public class Example11 {
RITIK KUMAR.
18SCSE1010646
public static void main(String[] args) {
Sample Solution:-
Java Code:
import java.util.*;
public class Example12 {
public static void main(String[] args) {
PriorityQueue<Integer> pq1 = new PriorityQueue<>(10, Collections.reverseOrder());
HashMap Exercises
1. Write a Java program to associate the specified value with the specified key in a HashMap.
Java Code:
import java.util.*;
public class Example1 {
public static void main(String args[]) {
HashMap<Integer,String> hash_map= new HashMap<Integer,String>();
hash_map.put(1, "Red");
hash_map.put(2, "Green");
hash_map.put(3, "Black");
hash_map.put(4, "White");
hash_map.put(5, "Blue");
for(Map.Entry x:hash_map.entrySet()){
System.out.println(x.getKey()+" "+x.getValue());
}
}
}
Output:-
2. Write a Java program to count the number of key-value (size) mappings in a map.
Java Code:
import java.util.*;
public class Example2 {
public static void main(String args[]){
RITIK KUMAR.
18SCSE1010646
HashMap<Integer,String> hash_map= new HashMap<Integer,String>();
hash_map.put(1, "Red");
hash_map.put(2, "Green");
hash_map.put(3, "Black");
hash_map.put(4, "White");
hash_map.put(5, "Blue");
System.out.println("Size of the hash map: "+hash_map.size());
}
}
Output:-
3. Write a Java program to copy all of the mappings from the specified map to another map.
Java Code:
import java.util.*;
public class Example3 {
public static void main(String args[]) {
// create two hash maps
HashMap <Integer,String> hash_map1 = new HashMap <Integer,String> ();
HashMap <Integer,String> hash_map2 = new HashMap <Integer,String> ();
// populate hash maps
hash_map1.put(1, "Red");
hash_map1.put(2, "Green");
hash_map1.put(3, "Black");
System.out.println("\nValues in first map: " + hash_map1);
hash_map2.put(4, "White");
hash_map2.put(5, "Blue");
hash_map2.put(6, "Orange");
System.out.println("\nValues in second map: " + hash_map2);
Java Code:
import java.util.*;
public class Example4 {
public static void main(String args[]) {
HashMap <Integer,String> hash_map = new HashMap <Integer,String> ();
hash_map.put(1, "Red");
RITIK KUMAR.
18SCSE1010646
hash_map.put(2, "Green");
hash_map.put(3, "Black");
hash_map.put(4, "White");
hash_map.put(5, "Blue");
// print the map
System.out.println("The Original linked map: " + hash_map);
// Removing all the elements from the linked map
hash_map.clear();
System.out.println("The New map: " + hash_map);
}
}
Output:-
5. Write a Java program to check whether a map contains key-value mappings (empty) or not.
Sample Solution:-
Java Code:
import java.util.*;
public class Example5 {
public static void main(String args[]) {
HashMap <Integer,String> hash_map = new HashMap <Integer,String> ();
hash_map.put(1, "Red");
hash_map.put(2, "Green");
hash_map.put(3, "Black");
hash_map.put(4, "White");
hash_map.put(5, "Blue");
// check if map is empty
boolean result = hash_map.isEmpty();
// check the result
System.out.println("Is hash map empty: " + result);
// Removing all the elements from the linked map
hash_map.clear();
// check if map is empty
result = hash_map.isEmpty();
// check the result
System.out.println("Is hash map empty: " + result);
}
}
Output:-
Sample Solution:-
Java Code:
import java.util.*;
public class Example6 {
RITIK KUMAR.
18SCSE1010646
public static void main(String args[]){
HashMap<Integer,String> hash_map= new HashMap<Integer,String>();
hash_map.put(1, "Red");
hash_map.put(2, "Green");
hash_map.put(3, "Black");
hash_map.put(4, "White");
hash_map.put(5, "Blue");
// print the map
System.out.println("The Original map: " + hash_map);
HashMap<Integer,String> new_hash_map= new HashMap<Integer,String>();
new_hash_map = (HashMap)hash_map.clone();
System.out.println("Cloned map: " + new_hash_map);
}
}
Output:-
7 . Write a Java program to test if a map contains a mapping for the specified key.
Java Code:
import java.util.*;
public class Example7 {
public static void main(String args[]) {
HashMap < String, Integer > hash_map = new HashMap < String, Integer > ();
hash_map.put("Red", 1);
hash_map.put("Green", 2);
hash_map.put("Black", 3);
hash_map.put("White", 4);
hash_map.put("Blue", 5);
// print the map
System.out.println("The Original map: " + hash_map);
System.out.println("1. Is key 'Green' exists?");
if (hash_map.containsKey("Green")) {
//key exists
System.out.println("yes! - " + hash_map.get("Green"));
} else {
//key does not exists
System.out.println("no!");
}
8.Write a Java program to test if a map contains a mapping for the specified value.
Sample Solution:-
Java Code:
import java.util.*;
public class Example8 {
public static void main(String args[]) {
HashMap < Integer, String > hash_map = new HashMap < Integer, String > ();
hash_map.put(1, "Red");
hash_map.put(2, "Green");
hash_map.put(3, "Black");
hash_map.put(4, "White");
hash_map.put(5, "Blue");
// print the map
System.out.println("The Original map: " + hash_map);
System.out.println("1. Is value \'Green\' exists?");
if (hash_map.containsValue("Green")) {
//value exists
System.out.println("Yes! ");
} else {
//value does not exists
System.out.println("no!");
}
9.Write a Java program to create a set view of the mappings contained in a map.
Java Code:
import java.util.*;
public class Example9 {
public static void main(String args[]) {
HashMap < Integer, String > hash_map = new HashMap < Integer, String > ();
RITIK KUMAR.
18SCSE1010646
hash_map.put(1, "Red");
hash_map.put(2, "Green");
hash_map.put(3, "Black");
hash_map.put(4, "White");
hash_map.put(5, "Blue");
// create set view for the map
Set set = hash_map.entrySet();
// check set values
System.out.println("Set values: " + set);
}
}
Output:-
10. Write a Java program to get the value of a specified key in a map.
Java Code:
import java.util.*;
public class Example10 {
public static void main(String args[]){
HashMap<Integer,String> hash_map= new HashMap<Integer,String>();
hash_map.put(1,"Red");
hash_map.put(2,"Green");
hash_map.put(3,"Black");
hash_map.put(4,"White");
hash_map.put(5,"Blue");
// get value of key 3
String val=(String)hash_map.get(3);
// check the value
System.out.println("Value for key 3 is: " + val);
}
Output:-
11.Write a Java program to get a set view of the keys contained in this map.
Java Code:
import java.util.*;
public class Example11 {
public static void main(String args[]){
hash_map.put(1,"Red");
hash_map.put(2,"Green");
hash_map.put(3,"Black");
hash_map.put(4,"White");
hash_map.put(5,"Blue");
RITIK KUMAR.
18SCSE1010646
// get keyset value from map
Set keyset = hash_map.keySet();
Java
Collection: TreeMap Exercises
import java.util.*;
public class Example2 {
public static void main(String args[]){
RITIK KUMAR. 18SCSE1010646
import java.util.*;
public class Example3 {
public static void main(String args[]){
TreeMap<String,String> tree_map1=new TreeMap<String,String>();
tree_map1.put("C1", "Red");
tree_map1.put("C2", "Green");
tree_map1.put("C3", "Black");
tree_map1.put("C4", "White");
System.out.println(tree_map1);
if(tree_map1.containsKey("C1")){
System.out.println("The Tree Map contains key C1");
} else {
RITIK KUMAR. 18SCSE1010646
import java.util.*;
public class Example4 {
public static void main(String args[]){
TreeMap<String,String> tree_map1=new TreeMap<String,String>();
tree_map1.put("C1", "Red");
tree_map1.put("C2", "Green");
tree_map1.put("C3", "Black");
tree_map1.put("C4", "White");
if(tree_map1.containsValue("Green")){
System.out.println("The TreeMap contains value Green");
} else {
System.out.println("The TreeMap does not contain value Green");
}
if(tree_map1.containsValue("Pink")){
System.out.println("The TreeMap contains value Pink");
} else {
RITIK KUMAR. 18SCSE1010646
5.Write a Java program to get all keys from a given Tree Map.
Solution:-
import java.util.*;
public class Example5 {
public static void main(String args[]){
TreeMap<String,String> tree_map1=new TreeMap<String,String>();
tree_map1.put("C1", "Red");
tree_map1.put("C2", "Green");
tree_map1.put("C3", "Black");
tree_map1.put("C4", "White");
Output
RITIK KUMAR. 18SCSE1010646
6. Write a Java program to delete all elements from a given Tree Map.
Solution:-
import java.util.*;
import java.util.Map.Entry;
public class Example6 {
public static void main(String args[]){ TreeMap<String,String>
tree_map1 = new TreeMap<String,String>(); tree_map1.put("C1", "Red");
tree_map1.put("C2", "Green");
tree_map1.put("C3", "Black");
tree_map1.put("C4", "White");
System.out.println(tree_map1);
}}
class sort_key implements Comparator<String>{
@Override
public int compare(String str1, String str2) {
return str1.compareTo(str2);
}}
Output
9. Write a Java program to get the first (lowest) key and the last
(highest) key currently in a map.
RITIK KUMAR. 18SCSE1010646
Solution:-
import java.util.*;
import java.util.Map.Entry;
public class Example9 {
public static void main(String args[]) {
TreeMap <String,String> tree_map1 = new TreeMap <String,String> ();
tree_map1.put("C2", "Red");
tree_map1.put("C1", "Green");
tree_map1.put("C4", "Black");
tree_map1.put("C3", "White");
System.out.println("Original TreeMap content: " + tree_map1);
System.out.println("Greatest key: " + tree_map1.firstKey());
System.out.println("Least key: " + tree_map1.lastKey());
}
}
Output
10. Write a Java program to get a reverse order view of the keys
contained in a given map.
Solution:-
import java.util.*;
import java.util.Map.Entry;
public class Example10 {
public static void main(String args[]) {
TreeMap <String,String> tree_map1 = new TreeMap <String,String> ();
tree_map1.put("C2", "Red");
tree_map1.put("C1", "Green");
tree_map1.put("C4", "Black");
tree_map1.put("C3", "White");
System.out.println("Original TreeMap content: " + tree_map1);
RITIK KUMAR. 18SCSE1010646
Solution:-
import java.util.*;
import java.util.Map.Entry;
public class Example11 {
public static void main(String args[]) {
TreeMap < Integer, String > tree_map1 = new TreeMap < Integer, String
> ();
tree_map1.put(10, "Red");
tree_map1.put(20, "Green");
tree_map1.put(40, "Black");
tree_map1.put(50, "White");
tree_map1.put(60, "Pink");
System.out.println("Original TreeMap content: " + tree_map1);
System.out.println("Checking the entry for 10: ");
System.out.println("Value is: " + tree_map1.floorEntry(10));
System.out.println("Checking the entry for 30: ");
System.out.println("Value is: " + tree_map1.floorEntry(30));
System.out.println("Checking the entry for 70: ");
System.out.println("Value is: " + tree_map1.floorEntry(70));
}
}
RITIK KUMAR. 18SCSE1010646
Output
12. Write a Java program to get the greatest key less than or equal to
the given key.
Solution:-
import java.util.*;
import java.util.Map.Entry;
public class Example12 {
public static void main(String args[]) {
TreeMap < Integer, String > tree_map1 = new TreeMap < Integer, String
> ();
tree_map1.put(10, "Red");
tree_map1.put(20, "Green");
tree_map1.put(40, "Black");
tree_map1.put(50, "White");
tree_map1.put(60, "Pink");
}}
Output
13.Write a Java program to get the portion of a map whose keys are
strictly less than a given key.
Solution:-
import java.util.*;
import java.util.Map.Entry;
public class Example13 {
public static void main(String args[]) {
TreeMap < Integer, String > tree_map1 = new TreeMap < Integer, String
> ();
tree_map1.put(10, "Red");
tree_map1.put(20, "Green");
tree_map1.put(40, "Black");
tree_map1.put(50, "White");
tree_map1.put(60, "Pink");
System.out.println("Original TreeMap content: " + tree_map1);
System.out.println("Checking the entry for 10: ");
System.out.println("Key(s): " + tree_map1.headMap(10));
System.out.println("Checking the entry for 30: ");
System.out.println("Key(s): " + tree_map1.headMap(30));
System.out.println("Checking the entry for 70: ");
System.out.println("Key(s): " + tree_map1.headMap(70));
}
}
RITIK KUMAR. 18SCSE1010646
Output
14. Write a Java program to get the portion of this map whose keys
are less than (or equal to, if inclusive is true) a given key.
Solution:-
import java.util.*;
import java.util.Map.Entry;
public class Example14 {
public static void main(String args[]) {
TreeMap < Integer, String > tree_map1 = new TreeMap < Integer, String
> ();
tree_map1.put(10, "Red");
tree_map1.put(20, "Green");
tree_map1.put(40, "Black");
tree_map1.put(50, "White");
tree_map1.put(60, "Pink");
System.out.println("Original TreeMap content: " + tree_map1);
System.out.println("Checking the entry for 10: ");
System.out.println("Key(s): " + tree_map1.headMap(10, true));
System.out.println("Checking the entry for 20: ");
System.out.println("Key(s): " + tree_map1.headMap(20, true));
System.out.println("Checking the entry for 70: ");
System.out.println("Key(s): " + tree_map1.headMap(70, true));
}
}
RITIK KUMAR. 18SCSE1010646
Output
15. Write a Java program to get the least key strictly greater than the
given key. Return null if there is no such key.
Solution:-
import java.util.*;
import java.util.Map.Entry;
public class Example15 {
public static void main(String args[]){
TreeMap< Integer, String > tree_map1 = new TreeMap< Integer, String
>();
tree_map1.put(10, "Red");
tree_map1.put(20, "Green");
tree_map1.put(40, "Black");
tree_map1.put(50, "White");
tree_map1.put(60, "Pink");
System.out.println("Original TreeMap content: "+tree_map1);
System.out.println("Checking the entry for 10: ");
System.out.println("Key(s): "+tree_map1.higherEntry(10));
System.out.println("Checking the entry for 20: ");
System.out.println("Key(s): "+tree_map1.higherEntry(20));
System.out.println("Checking the entry for 70: ");
System.out.println("Key(s): "+tree_map1.higherEntry(70));
}
}
Output
RITIK KUMAR. 18SCSE1010646
Solution:-
import java.util.*;
import java.util.Map.Entry;
public class Example16 {
public static void main(String args[]){
TreeMap< Integer, String > tree_map1 = new TreeMap< Integer, String
>();
tree_map1.put(10, "Red");
tree_map1.put(20, "Green");
tree_map1.put(40, "Black");
tree_map1.put(50, "White");
tree_map1.put(60, "Pink");
17. Write a Java program to get the greatest key strictly less than the
given key. Return null if there is no such key.
Solution:-
import java.util.*;
import java.util.Map.Entry;
public class Example17 {
public static void main(String args[]) {
TreeMap < Integer, String > tree_map1 = new TreeMap < Integer, String
> ();
tree_map1.put(10, "Red");
tree_map1.put(20, "Green");
tree_map1.put(40, "Black");
tree_map1.put(50, "White");
tree_map1.put(60, "Pink");
System.out.println("Original TreeMap content: " + tree_map1);
System.out.println("Checking the entry for 10: ");
System.out.println("Key(s): " + tree_map1.lowerKey(10));
System.out.println("Checking the entry for 20: ");
System.out.println("Key(s): " + tree_map1.lowerKey(20));
System.out.println("Checking the entry for 70: ");
System.out.println("Key(s): " + tree_map1.lowerKey(70));
}
}
Output
RITIK KUMAR. 18SCSE1010646
Solution:-
import java.util.*;
import java.util.Map.Entry;
public class Example18 {
public static void main(String args[]) {
TreeMap < Integer, String > tree_map1 = new TreeMap < Integer, String
> (); tree_map1.put(10, "Red");
tree_map1.put(20, "Green");
tree_map1.put(40, "Black");
tree_map1.put(50, "White");
tree_map1.put(60, "Pink");
System.out.println("Original TreeMap content: " + tree_map1);
System.out.println("Original TreeMap content: " +
tree_map1.navigableKeySet());
}
}
Output
RITIK KUMAR. 18SCSE1010646
Solution:-
import java.util.*;
import java.util.Map.Entry;
public class Example20 {
public static void main(String args[]) {
TreeMap < Integer, String > tree_map = new TreeMap < Integer, String >
();
tree_map.put(10, "Red");
tree_map.put(20, "Green");
tree_map.put(40, "Black");
tree_map.put(50, "White");
tree_map.put(60, "Pink");
System.out.println("Value before poll: " + tree_map);
System.out.println("Value returned: " + tree_map.pollLastEntry());
System.out.println("Value after poll: " + tree_map);
}}
Output
21. Write a Java program to get the portion of a map whose keys
range from a given key (inclusive), to another key (exclusive).
Solution:-
import java.util.*;
import java.util.Map.Entry;
public class Example21 {
public static void main(String args[]) {
TreeMap < Integer, String > tree_map = new TreeMap < Integer, String >
();
RITIK KUMAR. 18SCSE1010646
SortedMap < Integer, String > sub_tree_map = new TreeMap < Integer,
String > ();
tree_map.put(10, "Red");
tree_map.put(20, "Green");
tree_map.put(30, "Black");
tree_map.put(40, "White");
tree_map.put(50, "Pink");
System.out.println("Original TreeMap content: " + tree_map);
sub_tree_map = tree_map.subMap(20, 40);
System.out.println("Sub map key-values: " + sub_tree_map);
}
}
Output
22. Write a Java program to get the portion of a map whose keys
range from a given key to another key.
SampSolution:-
Java Code:
import java.util.*;
import java.util.Map.Entry;
public class Example22 {
public static void main(String args[]) {
TreeMap < Integer, String > tree_map = new TreeMap < Integer, String >
();
SortedMap < Integer, String > sub_tree_map = new TreeMap < Integer,
String > ();
tree_map.put(10, "Red");
tree_map.put(20, "Green");
tree_map.put(30, "Black");
tree_map.put(40, "White");
tree_map.put(50, "Pink");
RITIK KUMAR. 18SCSE1010646
23. Write a Java program to get a portion of a map whose keys are
greater than or equal to a given key.
Solution:-
import java.util.*;
import java.util.Map.Entry;
public class Example23 {
public static void main(String args[]) {
TreeMap < Integer, String > tree_map = new TreeMap < Integer, String >
();
tree_map.put(10, "Red");
tree_map.put(20, "Green");
tree_map.put(30, "Black");
tree_map.put(40, "White");
tree_map.put(50, "Pink");
System.out.println("Original TreeMap content: " + tree_map);
System.out.println("Keys are greater than or equal to 20: " +
tree_map.tailMap(20));
}
}
Output
RITIK KUMAR. 18SCSE1010646
24. Write a Java program to get a portion of a map whose keys are
greater than to a given key.
Solution:-
import java.util.*;
import java.util.Map.Entry;
public class Example24 {
public static void main(String args[]) {
TreeMap < Integer, String > tree_map = new TreeMap < Integer, String >
();
tree_map.put(10, "Red");
tree_map.put(20, "Green");
tree_map.put(30, "Black");
tree_map.put(40, "White");
tree_map.put(50, "Pink");
System.out.println("Original TreeMap content: " + tree_map);
System.out.println("Keys are greater than 20: " + tree_map.tailMap(20,
false));
}
}
Output
Solution:-
import java.util.*;
import java.util.Map.Entry;
public class Example25 {
public static void main(String args[]) {
RITIK KUMAR. 18SCSE1010646
TreeMap < Integer, String > tree_map = new TreeMap < Integer, String >
();
tree_map.put(10, "Red");
tree_map.put(20, "Green");
tree_map.put(30, "Black");
tree_map.put(40, "White");
tree_map.put(50, "Pink");
System.out.println("Original TreeMap content: " + tree_map);
System.out.println("Keys greater than or equal to 20: " +
tree_map.ceilingEntry(20));
System.out.println("Keys greater than or equal to 40: " +
tree_map.ceilingEntry(40));
System.out.println("Keys greater than or equal to 50: " +
tree_map.ceilingEntry(50));
}
}
Output
26. Write a Java program to get the least key greater than or equal to
the given key. Returns null if there is no such key.
Solution:-
import java.util.*;
import java.util.Map.Entry;
public class Example26 {
public static void main(String args[]) {
TreeMap < Integer, String > tree_map = new TreeMap < Integer, String >
();
tree_map.put(10, "Red");
tree_map.put(20, "Green");
tree_map.put(40, "Black");
RITIK KUMAR. 18SCSE1010646
tree_map.put(50, "White");
tree_map.put(60, "Pink");
System.out.println("Original TreeMap content: " + tree_map);
System.out.println("Keys greater than or equal to 20: " +
tree_map.ceilingKey(20));
System.out.println("Keys greater than or equal to 30: " +
tree_map.ceilingKey(30));
System.out.println("Keys greater than or equal to 50: " +
tree_map.ceilingKey(50));
}
}
Output