javaassignment1
javaassignment1
1:
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.
Program:-
import java.util.ArrayList;
import java.util.Scanner;
b) Write a java program to read 'n' names of your friends, store it into
linked list, also display contents of the same.
Program:-
import java.util.LinkedList;
import java.util.Scanner;
c) Write a program to create a new tree set, add some colors (string) and
print out the tree set.
Program:-
import java.util.TreeSet;
d) Create the hash table that will maintain the mobile number and
student name. Display the contact list.
Program:-
import java.util.Hashtable;
import java.util.Scanner;
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.
Program:-
import java.util.Scanner;
import java.util.TreeSet;
// Create a TreeSet to store integers (it will automatically sort and remove
duplicates)
TreeSet<Integer> numbers = new TreeSet<>();
// Ask the user how many student entries they want to add
System.out.print("Enter the number of students: ");
int numStudents = scanner.nextInt();
scanner.nextLine(); // Consume the newline character left by nextInt
// Load data from the text file into the hash tables
loadDataFromFile(filePath, nameToPhone, phoneToName);
// Method to load data from the file into the hash tables
private static void loadDataFromFile(String filePath, Hashtable<String, String>
nameToPhone, Hashtable<String, String> phoneToName) {
try {
// Create BufferedReader to read the file
BufferedReader reader = new BufferedReader(new FileReader(filePath));
String line;