0% found this document useful (0 votes)
8 views1 page

Java Collection Programs for Beginners

The document outlines a series of Java programming tasks focused on utilizing various collection types such as HashSet, LinkedList, and TreeSet. Each task requires the implementation of specific functionalities like adding, removing, and displaying elements, while ensuring no duplicates are stored. The tasks also emphasize the use of interfaces like Enumeration and Iterator for displaying collection contents.

Uploaded by

shravanphadke
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)
8 views1 page

Java Collection Programs for Beginners

The document outlines a series of Java programming tasks focused on utilizing various collection types such as HashSet, LinkedList, and TreeSet. Each task requires the implementation of specific functionalities like adding, removing, and displaying elements, while ensuring no duplicates are stored. The tasks also emphasize the use of interfaces like Enumeration and Iterator for displaying collection contents.

Uploaded by

shravanphadke
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

Collection

1) Write a java program to read ‘N’ names of your friends, store it into HashSet and
display them in ascending order. [15 M]

2) Write a Java program to create LinkedList of String objects and perform the following:
[Link] element at the end of the list
[Link] first element of the list
[Link] the contents of list in reverse order [15 M]

3) Write a Java program to store city names and their STD codes using an appropriate
collection and perform following operations:
[Link] a new city and its code (No duplicates)
[Link] a city from the collection
[Link] for a city name and display the code [15 M]

4) 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. [15 M]

5) 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. [15 M]

6) Write a java program to create a TreeSet, add some colors (String) and print out the
content of TreeSet in ascending order. [15 M]

7) 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. [15 M]

8) Write a java program to accept ‘N’ Integers from a user store them into LinkedList
Collection and display only negative integers. [15 M]

9) Write a java program to accept ‘N’ Subject Names from a user store them into
LinkedList Collection and Display them by using Iterator interface. [15 M]

10) 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. [15 M]

Common questions

Powered by AI

A LinkedList in Java can be manipulated by using the addLast() method to add elements to the end of the list, removeFirst() to delete the first element and using descendingIterator() to iterate in reverse order. LinkedList is a doubly-linked list implementation, which allows elements to be efficiently added or removed from both ends. The descendingIterator() provides an iterator that traverses the list in reverse sequential order.

A TreeSet in Java is a NavigableSet implementation that stores elements in a naturally ascending order. To use it for storing a collection of strings, simply add the strings to the TreeSet. The TreeSet automatically sorts them in natural order because it uses a tree for storage, guaranteeing that the sorted order is maintained. This makes it ideal for managing datasets where maintaining a natural sort order is required. It inherently prevents duplicate elements.

To filter and display only negative integers using a LinkedList, first store all user inputs in the LinkedList. Iterate through the list using an iterator or a simple for-loop, checking each integer. If an integer is negative, output it. LinkedList is suitable because it allows for efficient traversal and manipulation of lists. You can use its iterative capabilities to easily filter out desired numbers, such as negative integers, from the collection.

A HashMap would be appropriate for storing city names (as keys) and their STD codes (as values) because it efficiently handles the mapping of keys to values and allows for quick retrieval. To ensure no duplicate city names, the HashMap inherently prevents duplicate keys. To add a city, use the put() method, to remove a city, use remove(), and to search for a city, use the get() method. This setup efficiently supports operations like adding, removing, and searching for entries based on city names.

Use a TreeSet to store integers input by the user. The TreeSet ensures that duplicate integers are not stored, as it does not allow duplicate elements. Once all integers are added, the natural sorting of TreeSet will maintain them in ascending order. To display the integers, simply iterate over the TreeSet or convert it to another collection like a List if additional manipulation or display formatting is needed. TreeSet ensures both sorting and uniqueness through its algorithmic tree structure.

I recommend using the contains() method provided by many Java Collections classes like List, Set, and Map, for efficiently searching for an element within a collection. This method checks if a specified element exists in the collection and returns a boolean result. When working with ordered collections like TreeSet, binary search algorithms can also be utilized if the data is sorted beforehand, enabling faster search operations.

Subject names can be stored in a LinkedList, which provides efficient insertion and deletion capabilities. To iterate over the stored names, use the LinkedList's iterator() method to get an Iterator. This allows for straightforward element traversal with operations like hasNext() and next() to access each subject name. A ListIterator could also be employed to navigate in both forward and backward directions, adjust elements, and maintain a current position. Using LinkedLists is beneficial in scenarios necessitating frequent modifications to the list.

To implement a Java program that stores 'N' unique names and displays them in ascending order, you can use a HashSet to store the names initially, as it does not allow duplicates. Then, you can convert the HashSet to an ArrayList, and sort it using Collections.sort() to display the names in ascending order. HashSet is selected because it provides constant-time performance for the basic operations (add, remove, contains, and size)

The Enumeration interface in Java provides methods to enumerate (i.e., iterate) through a collection of elements, one at a time. It can be used to display student details from a hash table by first obtaining the keys or entries in the hash table, and then using the hasMoreElements() and nextElement() methods to iterate through them and print the required details. Enumeration is considered a legacy interface and is less used compared to iterators, but can be useful with older data structures like Vector or Hashtable.

Design a Java program that begins by checking command-line arguments to count 'N' student names. Store these names in a List (like an ArrayList for random access). Use an Iterator obtained from calling iterator() to display the names in sequence. For bidirectional navigation, obtain a ListIterator using listIterator() to iterate and possibly modify the list while traversing in both directions. This combination of Iterators provides flexibility in how the data is accessed and manipulated, ideal for tasks requiring varied iteration patterns.

You might also like