Set A :
a) Write a java program to accept names of n cities, insert same into array list collection and
display the contents of same array list, also remove all these elements.
import [Link].*;
public class A1
{
public static void main(String[] args)
{
ArrayList al = new ArrayList<> ();
Scanner sc = new Scanner([Link]);
[Link]("Enter How many cities :");
int n = [Link]();
[Link]("Enter the Cities :");
for (int i = 0; i < n; i++)
{
String c = [Link]();
[Link](c);
}
[Link]("Cities :" + al);
[Link]("ArrayList after removing the elements :");
[Link]();
[Link]();
}
}
b) Write a java program to read n names of your friends, store it into linked list, also display
contents of the same.
import [Link].*;
public class A2
{
public static void main(String[] args)
{
LinkedList ll = new LinkedList<> ();
Scanner sc = new Scanner ([Link]);
[Link]("Enter How many Friends :");
int n = [Link]();
[Link]("Enter the "+n+" Friends :");
for (int i = 0; i < n; i++)
{
String fl = [Link]();
[Link](fl);
}
[Link]("Friends :" + ll);
[Link]();
}
}
c) Write a program to create a new tree set, add some colors (string) and print out the tree set.
import [Link];
import [Link];
public class A3
public static void main(String[] args)
{
Scanner sc = new Scanner([Link]);
TreeSet<String> ts = new TreeSet<>();
[Link]("Enter How many Colours :");
int n = [Link]();
[Link]("Enter the Colours :");
for (int i = 0; i < n; i++)
{
String c = [Link]();
[Link](c);
}
[Link]("Colours :" + ts);
[Link]();
}
}
d) Create the hash table that will maintain the mobile number and student name. Display the
contact list.
import [Link];
public class A4
{
public static void main(String[] args)
{
Hashtable<String, String> ht = new Hashtable<>();
[Link]("Priya", "8796465800");
[Link]("Anisha", "8806503414");
[Link]("Sushma", "8629913414");
[Link]("Swapna", "7118919895");
[Link](ht);
}
}
Set B:
a) Accept 'n' integers from the user. Store and display integers in sorted order having proper
collection class. The collection should not accept duplicate elements.
import [Link];
import [Link];
public class B1
{
public static void main(String[] args)
{
TreeSet ts = new TreeSet<>();
Scanner sc = new Scanner ([Link]);
[Link]("Enter how many Numbers: ");
int n = [Link]();
[Link]("Enter the Numbers: ");
for (int i = 0; i < n; i++)
{
int num = [Link]();
[Link](num);
}
[Link]("Numbers in Sorted Order and without Duplication :" + ts);
[Link]();
}
}
b) Write a program to sort HashMap by keys and display the details before sorting and after
sorting.
import [Link];
import [Link];
public class B2
{
public static void main(String[] args)
{
HashMap<String, Integer> hm = new HashMap<>();
[Link]("Priya", 11);
[Link]("Anisha", 2);
[Link]("Sushma", 5);
[Link]("Swapna", 10);
[Link]("\nHashMap Details Before Sorting :" + hm);
TreeMap tm = new TreeMap<>(hm);
[Link]("\nHashMap Details After Sorting :\n" + tm);
}
}
Set C:
b) Write a program to create link list of integer objects. Do the following:
i. add element at first position
ii. delete last element
iii. display the size of link list
import [Link];
import [Link];
public class C2
{
public static void main(String[] args)
{
Scanner sc = new Scanner([Link]);
LinkedList ll = new LinkedList<>();
[Link](1);
[Link](2);
[Link](3);
[Link]("\nElements in List :\n" + ll);
[Link](0);
[Link]("\nList after adding Elements at First :\n" + ll);
[Link]();
[Link]("\nList after deleting Last Element :\n" + ll);
[Link]("\nSize of the List :\n" + [Link]());
[Link]();
}
}