import java.util.*;
class LinkedHashSet_TechSarvam
{
public static void main(String args[])
{
// Creating a LinkedHashSet
LinkedHashSet<String> al = new LinkedHashSet<String>();
// Adding elements to the LinkedHashSet
al.add("Rajendra");
al.add("Raja");
al.add("Ravi");
al.add("TechSarvam");
// Creating an iterator for LinkedHashSet
Iterator<String> itr = al.iterator();
// Iterating through the elements of LinkedHashSet
while (itr.hasNext())
{
System.out.println(itr.next()); // Printing each element
}
}
}
Step-by-Step Explanation:
1. Imported Package:
import java.util.*;
It has imported classes that are from java.util, so LinkedHashSet, Iterator etc.
2. Declaring Class
class LinkedHashSet_TechSarvam {
Declares a class linked to LinkedHashSet_TechSarvam, which has included the main method. It will be the start of the application.
3. Main Method :
public static void main(String args[]) {
The main() method is where the program starts its execution.
4. Creating a LinkedHashSet:
LinkedHashSet<String> al = new LinkedHashSet<String>();
A LinkedHashSet named al is declared to store String elements. A LinkedHashSet is similar to a HashSet but keeps track of the order in which elements were inserted.
5. Adding Elements to the LinkedHashSet:
al.add(“Rajendra”);
al.add(“Raja”);
al.add(“Ravi”);
al.add(“TechSarvam”)
- Here, the add() method is used to add elements to the LinkedHashSet. The elements added here are “Rajendra”, “Raja”, “Ravi”, and “TechSarvam”.
Important Note: LinkedHashSet maintains the order of insertion. Thus, the order of retrieval while iteration will be the same as that of insertion.
6. Creating an Iterator to Iterate Over the LinkedHashSet:
Iterator<String> itr = al.iterator();
An Iterator is created for the LinkedHashSet so that its elements can be iterated over. The iterator() method returns an iterator that can be used to iterate over the set.
7. Iteration of the LinkedHashSet:
while (itr.hasNext())
out itr // (This line has an error, needs correction)
}
- The while loop checks if there are more elements left in the LinkedHashSet using itr.hasNext().
- The itr.next() method is used to retrieve and print the next element in the LinkedHashSet.
- Wrong Code: The line out itr is wrong and the code will give a compilation error. The right code is System.out.println(itr.next());
Explanation of the Code:
1. LinkedHashSet Behavior:
- The LinkedHashSet maintains the insertion order. Therefore, the elements are iterated in the same order in which they are added to the set.
- If you try to insert the same element again, LinkedHashSet will not allow duplicate entries. But in this program that is not a problem because all the inserted elements are unique.
2. Iteration Traversal:
An Iterator object is used here for traversal through LinkedHashSet and prints each element
- hasNext() method is used to see if there is any more element in the set.
- The next() method returns the next element from the set.
3. Improved Output Statement:
The statement out itr is syntactically incorrect and actually it prints the next element in the set. Replaces System.out.println(itr.next());
Expected Output
The LinkedHashSet ordered by the order of its insertion, so it will print,
Rajendra
Raja
Ravi
TechSarvam
On the next line of every element on which they are inserted.
Key Points
1. LinkedHashSet:
- A class implementing the Set interface. This is similar to a HashSet except that it recalls the order that elements were entered.
- Won’t accept duplications. For this reason, if you’re trying to insert the same object again, then it won’t add it to the set
2. Iterator:
Iterator to work over collections like LinkedHashSet. These methods include hasNext() to see if more elements are available and next() to get the next element from the collection.
Summary:
This creates a LinkedHashSet, populates it with some elements, and then iterates over its contents with an Iterator printing each element. This code has been kept fully functional to print all the elements in the order that they were inserted into the set because a LinkedHashSet keeps track of an insertion order.