3.Write a Java program to check if a particular element exists in a linked list.
CODE
import java.util.LinkedList;
public class Main3
{
public static void main(String[] args)
{
LinkedList <String> arr=new LinkedList<String>();
arr.add("A");
arr.add("B");
arr.add("C");
arr.add("D");
arr.add("E");
System.out.println("Original: "+arr);
if(arr.contains("A")){
System.out.println("A - The element exists");
}
else{
System.out.println("A - The element does not exists");
}
if(arr.contains("I")){
System.out.println("I - The element exists");
}
else{
System.out.println("I - The element does not exists");
}
}
}
OUTPUT
4.Write a Java program to remove all of the elements from a hash set.
CODE
import java.util.HashSet;
public class Main4
{
public static void main(String[] args)
{
HashSet<String> arr=new HashSet<>();
arr.add("A");
arr.add("B");
arr.add("C");
arr.add("D");
arr.add("E");
System.out.println("Original: "+arr);
arr.clear();
System.out.println("Removed all the elements from the list: "+arr);
}
}
OUTPUT
5.Write a Java program to compare two hash set.
CODE
import java.util.HashSet;
public class Main5 {
public static void main(String[] args) {
HashSet<String> arr=new HashSet<>();
arr.add("A");
arr.add("B");
arr.add("C");
arr.add("D");
arr.add("E");
System.out.println("Set 1: "+arr);
HashSet<String> arr1=new HashSet<>();
arr1.add("A");
arr1.add("L");
arr1.add("C");
arr1.add("J");
arr1.add("E");
System.out.println("Set 2: "+arr1);
boolean isEquals = arr.equals(arr1);
System.out.println("By comparing two set is set1 and set2 equal? : "+isEquals);
}
}
OUTPUT
6.Write a Java program to retrieve and remove the last element of a tree set.
CODE
import java.util.TreeSet;
public class Main7 {
public static void main(String[] args) {
TreeSet<String> arr = new TreeSet<>();
arr.add("A");
arr.add("B");
arr.add("C");
arr.add("D");
arr.add("E");
System.out.println("Original:" + arr);
System.out.println("Removing the last element: "+arr.pollLast());
System.out.println("Tree set after removing last element:"+arr);
}
}
OUTPUT
7.Write a Java program to convert a Priority Queue elements to a string representation.
CODE
import java.util.PriorityQueue;
public class Main8 {
public static void main(String[] args) {
PriorityQueue<String> arr = new PriorityQueue<String>();
arr.add("Apple");
arr.add("Bomb");
arr.add("Cat");
arr.add("Door");
arr.add("Eat");
arr.add("Fan");
System.out.println("Original:"+arr);
String x = arr.toString();
System.out.println("String representation: "+x);
}
}
OUTPUT
8.Write a Java program to get a collection view of the values contained in this map.
CODE
import java.util.HashMap;
public class Main9 {
public static void main(String args[]){
HashMap<Integer,String> arr= new HashMap<Integer,String>();
arr.put(1,"A");
arr.put(2,"B");
arr.put(3,"C");
arr.put(4,"D");
arr.put(5,"E");
System.out.println("Collection view: "+ arr.values());
}
}
OUTPUT
9.Write a Java program to get a set view of the keys contained in this map
CODE
import java.util.HashMap;
import java.util.Set;
public class Main10 {
public static void main(String args[]){
HashMap<Integer,String> arr= new HashMap<Integer,String>();
arr.put(1,"A");
arr.put(2,"B");
arr.put(3,"C");
arr.put(4,"D");
arr.put(5,"E");
Set x = arr.keySet();
System.out.println("set view of the keys contained in this map: "+x);
}
}
OUTPUT
10.Write a Java program to get a key-value mapping associated with the least key greater than or equal to the given key. Return null if there is no such key.
CODE
import java.util.*;
public class Main11 {
public static void main(String args[]) {
TreeMap < Integer, String > arr = new TreeMap < Integer, String > ();
arr.put(1,"A");
arr.put(2,"B");
arr.put(3,"C");
arr.put(4,"D");
arr.put(5,"E");
System.out.println("Original: " + arr);
System.out.println("Keys greater than or equal to 1: " + arr.ceilingEntry(1));
System.out.println("Keys greater than or equal to 3: " + arr.ceilingEntry(3));
System.out.println("Keys greater than or equal to 6: " + arr.ceilingEntry(6));
}
}
OUTPUT
11.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.
CODE
import java.util.*;
public class Main12 {
public static void main(String args[]) {
TreeMap < Integer, String > arr = new TreeMap < Integer, String > ();
arr.put(1,"A");
arr.put(2,"B");
arr.put(3,"C");
arr.put(4,"D");
arr.put(5,"E");
System.out.println("Original: " + arr);
System.out.println("Keys greater than or equal to 1: " + arr.ceilingKey(1));
System.out.println("Keys greater than or equal to 3: " + arr.ceilingKey(3));
System.out.println("Keys greater than or equal to 6: " + arr.ceilingKey(6));
}
}