Java Solved
Java Solved
1. Write a java program to read ‘N’ names of your friends, store it into HashSet and display
them in ascending order.
Program:
import java.util.*;
Explanation:
1. The program starts by importing the necessary classes - java.util.*.
2. The user is prompted to enter the number of friends to store in the HashSet.
3. A HashSet friends is created to store the names of friends.
4. Using a for loop, the program reads 'n' names of friends from the user and adds them
to the HashSet.
5. To display the names in ascending order, the HashSet is converted to an ArrayList
sortedFriends, which can be sorted using the Collections.sort() method.
6. Finally, the sorted names are displayed using a for-each loop.
Note: HashSet does not maintain the order of elements, hence we have used an ArrayList to
sort the elements.
Slip 3
2. Write a Java program to create LinkedList of String objects and perform the following:
i. Add element at the end of the list.
ii. Delete first element of the list.
iii. Display the contents of list in reverse order.
Program:
import java.util.LinkedList;
Slip 4
2. Write a Java program to store city names and their STD codes using an appropriate
collection and perform following operations:
i. Add a new city and its code (No duplicates).
ii. Remove a city from the collection.
iii. Search for a city name and display the code.
Program:
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
Slip 5
1. Write a Java Program to create the hash table that will maintain the mobile number and
student name. Display the details of student using Enumeration interface.
Program:
import java.util.*;
System.out.println(hashtable);
Enumeration<String> e = hashtable.keys();
while (e.hasMoreElements()) {
String studentName = e.nextElement();
Long mobileNumber = hashtable.get(studentName);
System.out.println("Student Name: " + studentName);
System.out.println("Mobile Number: " + mobileNumber);
System.out.println();
}
}
}
Explanation:
1. The program creates a Hashtable that maps student names to mobile numbers. The
keys of the Hashtable are student names and the values are mobile numbers.
2. The program adds some sample student details to the Hashtable. Note that the mobile
numbers are now being stored as integers.
3. The Enumeration interface is used to iterate over the keys of the Hashtable using the
keys() method, which returns an enumeration of the keys in the Hashtable.
4. The hasMoreElements() method of Enumeration is used to check if there are more
keys in the enumeration, and the nextElement() method is used to retrieve the next
key.
5. For each student name, the program retrieves the corresponding mobile number using
the get() method of the Hashtable and displays the details of the student.
Slip 6
1. Write a Java program to accept ‘n’ integers from the user and store them in a
collection. Display them in the sorted order. The collection should not accept
duplicate elements. (Use a suitable collection). Search for a particular element using
predefined search method in the Collection framework.
Program:
import java.util.Scanner;
import java.util.Set;
import java.util.TreeSet;
// Accept n integers from the user and add them to the set
System.out.print("Enter the number of integers: ");
int n = sc.nextInt();
System.out.print("Enter " + n + " integers: ");
for (int i = 0; i < n; i++) {
int num = sc.nextInt();
numbers.add(num);
}
Program:
import java.util.*;
Explanation:
Slip 17
1. Write a java program to accept ‘N’ integers from a user. Store and display integers in
sorted order having proper collection class. The collection should not accept duplicate
elements.
Program :
import java.util.*;
System.out.println("Sorted integers:");
for (int num : set) {
System.out.println(num);
}
}
}
Slip 19
1. Write a java program to accept ‘N’ Integers from a user store them into LinkedList
Collection and display only negative integers.
Program:
import java.util.*;
System.out.println("Negative integers:");
for (int num : list) {
if (num < 0) {
System.out.println(num);
}
}
}
}
Slip 21
1. Write a java program to accept ‘N’ Subject Names from a user store them into
LinkedList Collection and Display them by using Iterator interface.
Program:
import java.util.*;
System.out.println("Subject names:");
Iterator<String> iterator = list.iterator();
while (iterator.hasNext()) {
System.out.println(iterator.next());
}
}
}
Slip 23
1. Write a java program to accept ‘N’ student names through command line, store
them into the appropriate Collection and display them by using Iterator and
ListIterator interface.
Program: