🧪 Practice Question Set
🔹 Level 1: Basics
1. Create a HashSet of integers and add numbers 1 to 5. Print the set.
2. Try adding a duplicate element to the HashSet. What happens?
3. Check if HashSet allows null values.
4. Iterate through a HashSet using for-each loop.
5. Create a LinkedHashSet of Strings and observe the insertion order.
6. Add and remove an element from a LinkedHashSet. Print the set before and
after.
7. Create a TreeSet and add elements: 30, 10, 40, 20. Print the set and observe
the order.
🔹 Level 2: Intermediate
8. Create a TreeSet of Strings and add some names. Print them in sorted order.
9. Check if TreeSet allows null. What exception do you get?
10.Create a HashSet and convert it into a TreeSet.
11.Write a program to find the union of two sets using HashSet.
12.Write a program to find the intersection of two sets using retainAll().
13.Write a program to remove all elements from a TreeSet one by one using
iterator.
🔹 Level 3: Applied / Coding Style
14.Check if two sets are equal (same elements regardless of order).
15.Check if one set is a subset of another.
16.Given a list of student names with duplicates, return a list of unique names
maintaining insertion order.
17.Write a method that removes duplicate characters from a string using
LinkedHashSet.
18.Use TreeSet to sort and store custom objects like Student with fields id,
name, implementing Comparable.
19.Use TreeSet with custom comparator to sort employees by salary.
20.Given two TreeSets, find elements present in the first but not in the second.