0% found this document useful (0 votes)
5 views4 pages

Java 1

The document contains Java programming exercises focused on using different collection classes such as ArrayList, LinkedList, TreeSet, and Hashtable. It includes tasks like accepting user input for cities and friends, displaying sorted integers without duplicates, and sorting a HashMap by keys. Each exercise is accompanied by sample code demonstrating the implementation of the required functionality.

Uploaded by

Nikita Pawar
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)
5 views4 pages

Java 1

The document contains Java programming exercises focused on using different collection classes such as ArrayList, LinkedList, TreeSet, and Hashtable. It includes tasks like accepting user input for cities and friends, displaying sorted integers without duplicates, and sorting a HashMap by keys. Each exercise is accompanied by sample code demonstrating the implementation of the required functionality.

Uploaded by

Nikita Pawar
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

Practical 1

Class: TyBCS Sub: Java-II


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].*;
class CityArrayList {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
ArrayList<String> cities = new ArrayList<>();
[Link]("Enter number of cities: ");
int n = [Link]();
[Link](); // consume newline
for (int i = 0; i < n; i++) {
[Link]("Enter city name: ");
[Link]([Link]());
}
[Link]("Cities in ArrayList: " + cities);
[Link](); // remove all elements
[Link]("After removing all cities: " + cities);
}
}

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].*;
class FriendLinkedList {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
LinkedList<String> friends = new LinkedList<>();
[Link]("Enter number of friends: ");
int n = [Link]();
[Link]();
for (int i = 0; i < n; i++) {
[Link]("Enter friend name: ");
[Link]([Link]());
}
[Link]("Friends List:");
for (String name : friends) {
[Link](name);
}
}
}

c) Write a program to create a new tree set, add some colors (string) and print out the
tree set.
import [Link].*;
class ColorTreeSet {
public static void main(String[] args) {
TreeSet<String> colors = new TreeSet<>();
[Link]("Red");
[Link]("Blue");
[Link]("Green");
[Link]("Yellow");
[Link]("Colors in TreeSet:");
for (String c : colors) {
[Link](c);
}
}
}
d) Create the hash table that will maintain the mobile number and student name.
Display the contact list.
import [Link].*;
class StudentHashtable {
public static void main(String[] args) {
Hashtable<String, String> contacts = new Hashtable<>();
[Link]("Amit", "9876543210");
[Link]("Neha", "9123456780");
[Link]("Ravi", "9988776655");
[Link]("Student Contact List:");
for ([Link]<String, String> entry : [Link]()) {
[Link]("Name: " + [Link]() +
" Mobile: " + [Link]());
}
}
}

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].*;
class SortedIntegers {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
TreeSet<Integer> numbers = new TreeSet<>();
[Link]("Enter number of integers: ");
int n = [Link]();
for (int i = 0; i < n; i++) {
[Link]("Enter integer: ");
[Link]([Link]()); // duplicates ignored
}
[Link]("Sorted integers (No duplicates):");
for (int num : numbers) {
[Link](num);
}
}
}

b) Write a program to sort HashMap by keys and display the details before sorting and
after sorting.
import [Link].*;
class SortHashMap {
public static void main(String[] args) {
HashMap<Integer, String> map = new HashMap<>();
[Link](3, "Java");
[Link](1, "Python");
[Link](2, "C++");
[Link]("Before Sorting:");
for ([Link]<Integer, String> entry : [Link]()) {
[Link]([Link]() + " : " + [Link]());
}
// Sorting HashMap by keys
TreeMap<Integer, String> sortedMap = new TreeMap<>(map);
[Link]("\nAfter Sorting by Keys:");
for ([Link]<Integer, String> entry : [Link]()) {
[Link]([Link]() + " : " + [Link]());
}
}
}

You might also like