0% found this document useful (0 votes)
18 views

Collection_Slip_Answers

The document contains multiple Java programs demonstrating the use of different collections such as LinkedList, Hashtable, TreeSet, and HashMap. Each program performs specific operations like accepting user input, storing data, and displaying or manipulating the data in various ways. Key functionalities include managing subject names, student details, unique integers, negative integers, friend names, colors, and city STD codes.

Uploaded by

VENOM King
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views

Collection_Slip_Answers

The document contains multiple Java programs demonstrating the use of different collections such as LinkedList, Hashtable, TreeSet, and HashMap. Each program performs specific operations like accepting user input, storing data, and displaying or manipulating the data in various ways. Key functionalities include managing subject names, student details, unique integers, negative integers, friend names, colors, and city STD codes.

Uploaded by

VENOM King
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

/* Write a java program to accept 'N' Subject Names from a user store them into LinkedList Collection and

Display
them by using Iterator interface.

*/
import java.util.*;

public class SubjectNamesLinkedList {


public static void main(String[] args) {
// Create a LinkedList to store subject names
LinkedList<String> subjectList = new LinkedList<>();

// Scanner to read user input


Scanner scanner = new Scanner(System.in);

// Accept 'N' subject names from the user


System.out.print("Enter the number of subjects you want to input: ");
int n = scanner.nextInt();
scanner.nextLine(); // Consume the newline character after integer input

// Input 'N' subject names and store them in the LinkedList


System.out.println("Enter the subject names:");
for (int i = 0; i < n; i++) {
String subject = scanner.nextLine();
subjectList.add(subject); // Add the subject name to the LinkedList
}

// Create an Iterator to traverse through the LinkedList


Iterator<String> iterator = subjectList.iterator();

// Display the subject names using the Iterator interface


System.out.println("\nSubjects in the list:");
while (iterator.hasNext()) {
System.out.println(iterator.next());
}

// Close the scanner


scanner.close();
}
}
/*
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.
*/

import java.util.*;

public class StudentMobileDetails {


public static void main(String[] args) {
// Create a Hashtable to store student names and mobile numbers
Hashtable<String, String> studentTable = new Hashtable<>();

// Adding student names and mobile numbers


studentTable.put("Alice", "9876543210");
studentTable.put("Bob", "9123456789");
studentTable.put("Charlie", "9345678901");
studentTable.put("David", "9054321098");

// Display student details using Enumeration interface


System.out.println("Student Details (Name - Mobile Number):");

// Get the enumeration for keys (names)


Enumeration<String> names = studentTable.keys();

// Get the enumeration for values (mobile numbers)


Enumeration<String> numbers = studentTable.elements();

// Print the details by iterating over both the name and mobile number using Enumeration
while (names.hasMoreElements()) {
String name = names.nextElement();
String number = numbers.nextElement();
System.out.println(name + " - " + number);
}
}
}
/*
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
*/
import java.util.*;

public class SortedUniqueIntegers {


public static void main(String[] args) {
// Create a TreeSet to store integers (automatically sorted and no duplicates)
Set<Integer> numberSet = new TreeSet<>();

// Scanner to read user input


Scanner scanner = new Scanner(System.in);

// Accept 'n' integers from the user


System.out.print("Enter the number of integers you want to input: ");
int n = scanner.nextInt();

// Input n integers and store them in the TreeSet


System.out.println("Enter the integers:");
for (int i = 0; i < n; i++) {
int num = scanner.nextInt();
numberSet.add(num); // TreeSet automatically handles duplicates
}

// Display the integers in sorted order


System.out.println("\nSorted integers without duplicates:");
for (Integer number : numberSet) {
System.out.println(number);
}

// Search for a particular element


System.out.print("\nEnter the integer to search for: ");
int searchNum = scanner.nextInt();
if (numberSet.contains(searchNum)) {
System.out.println(searchNum + " is present in the collection.");
} else {
System.out.println(searchNum + " is not present in the collection.");
}

// Close the scanner


scanner.close();
}
}
/*
Write a java program to accept 'N' Integers from a user store them into LinkedList Collection and display only
negative integers.
*/

import java.util.*;

public class NegativeIntegersLinkedList {


public static void main(String[] args) {
// Create a LinkedList to store integers
LinkedList<Integer> integerList = new LinkedList<>();

// Scanner to read user input


Scanner scanner = new Scanner(System.in);

// Accept 'N' integers from the user


System.out.print("Enter the number of integers you want to input: ");
int n = scanner.nextInt();

// Input 'N' integers and store them in the LinkedList


System.out.println("Enter the integers:");
for (int i = 0; i < n; i++) {
int num = scanner.nextInt();
integerList.add(num); // Add the integer to the LinkedList
}

// Display only negative integers


System.out.println("\nNegative integers are:");
boolean foundNegative = false;
for (Integer number : integerList) {
if (number < 0) {
System.out.println(number);
foundNegative = true;
}
}

// If no negative integers are found


if (!foundNegative) {
System.out.println("No negative integers found.");
}

// Close the scanner


scanner.close();
}
}
/*
Write a Java program to create LinkedList of String objects and perform the following:

1)Add element at the end of the list


2)Delete first element of the list.
3)Display the contents of list in reverse order
*/

import java.util.*;

public class LinkedListExample {


public static void main(String[] args) {
// Create a LinkedList of String objects
LinkedList<String> list = new LinkedList<>();

// Step 1: Add elements at the end of the list


list.add("Alice");
list.add("Bob");
list.add("Charlie");
list.add("David");

System.out.println("List after adding elements at the end: " + list);

// Step 2: Delete the first element of the list


if (!list.isEmpty()) {
list.removeFirst(); // Removes the first element
System.out.println("List after deleting the first element: " + list);
} else {
System.out.println("List is empty, cannot remove first element.");
}

// Step 3: Display the contents of the list in reverse order


System.out.println("List in reverse order:");
Iterator<String> iterator = list.descendingIterator(); // Iterator for reverse order
while (iterator.hasNext()) {
System.out.println(iterator.next());
}
}
}
/*
Write a java program to read 'N' names of your friends, store it into HashSet and display them in ascending order.
*/
import java.util.*;

public class FriendNames {


public static void main(String[] args) {
// Scanner to read user input
Scanner scanner = new Scanner(System.in);

// Ask the user how many names they want to enter


System.out.print("Enter the number of friends: ");
int N = scanner.nextInt();
scanner.nextLine(); // To consume the newline character left by nextInt()

// Create a HashSet to store the names (HashSet removes duplicates)


Set<String> friendsSet = new HashSet<>();

// Loop to read N names from the user


System.out.println("Enter the names of your friends:");
for (int i = 0; i < N; i++) {
String name = scanner.nextLine();
friendsSet.add(name); // Add name to the HashSet
}

// Convert the HashSet to a List and sort it in ascending order


List<String> sortedList = new ArrayList<>(friendsSet);
Collections.sort(sortedList);

// Display the names in ascending order


System.out.println("\nFriends' names in ascending order:");
for (String name : sortedList) {
System.out.println(name);
}

// Close the scanner


scanner.close();
}
}
/*
Write a java program to create a TreeSet, add some colors (String) and print out the content of TreeSet in ascending
order.
*/
import java.util.*;

public class ColorTreeSet {


public static void main(String[] args) {
// Create a TreeSet to store color names (Strings)
Set<String> colorSet = new TreeSet<>();

// Add some colors to the TreeSet


colorSet.add("Red");
colorSet.add("Blue");
colorSet.add("Green");
colorSet.add("Yellow");
colorSet.add("Pink");
colorSet.add("Black");

// Print the content of the TreeSet in ascending order


System.out.println("Colors in ascending order:");
for (String color : colorSet) {
System.out.println(color);
}
}
}

=================================================================================================
/*
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

*/
import java.util.*;

public class CitySTDCode {


public static void main(String[] args) {
// Create a HashMap to store city names and their STD codes
Map<String, String> citySTDMap = new HashMap<>();

// Add some initial data


citySTDMap.put("Mumbai", "022");
citySTDMap.put("Delhi", "011");
citySTDMap.put("Bangalore", "080");
// Print the initial cities and their codes
System.out.println("Initial City STD Codes: " + citySTDMap);

// i. Add a new city and its code (No duplicates)


addCity(citySTDMap, "Chennai", "044");
addCity(citySTDMap, "Mumbai", "022"); // Duplicate city, will not be added
System.out.println("\nAfter adding cities: " + citySTDMap);

// ii. Remove a city from the collection


removeCity(citySTDMap, "Delhi");
System.out.println("\nAfter removing Delhi: " + citySTDMap);

// iii. Search for a city name and display the code


searchCity(citySTDMap, "Bangalore");
searchCity(citySTDMap, "Delhi");
}

// Method to add a city and its code (no duplicates)


public static void addCity(Map<String, String> map, String city, String code) {
if (!map.containsKey(city)) {
map.put(city, code);
System.out.println("Added: " + city + " with STD code " + code);
} else {
System.out.println("City " + city + " already exists with STD code " + map.get(city));
}
}

// Method to remove a city from the collection


public static void removeCity(Map<String, String> map, String city) {
if (map.containsKey(city)) {
map.remove(city);
System.out.println("Removed: " + city);
} else {
System.out.println("City " + city + " not found in the collection.");
}
}

// Method to search for a city and display its STD code


public static void searchCity(Map<String, String> map, String city) {
if (map.containsKey(city)) {
System.out.println("STD code for " + city + " is: " + map.get(city));
} else {
System.out.println("City " + city + " not found in the collection.");
}
}
}

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy