1. What is a LinkedHashSet in Java?
Answer:
Explanation:
LinkedHashSet is a HashSet with a linked list running through its entries to maintain order.
2. How does LinkedHashSet maintain insertion order?
Answer:
Explanation:
LinkedHashSet maintains insertion order through a doubly-linked list across its elements.
3. What interfaces does LinkedHashSet implement?
Answer:
Explanation:
LinkedHashSet implements the Set interface.
4. Can a LinkedHashSet contain duplicate elements?
Answer:
Explanation:
LinkedHashSet does not allow duplicate elements, similar to HashSet.
5. Which method do you use to add elements in a LinkedHashSet?
Answer:
Explanation:
Elements are added to a LinkedHashSet using the add() method.
6. Does LinkedHashSet allow null elements?
Answer:
Explanation:
LinkedHashSet allows null elements.
7. How do you remove an element from a LinkedHashSet?
Answer:
Explanation:
The remove() method is used to remove an element from a LinkedHashSet.
8. What is the initial default capacity of a LinkedHashSet?
Answer:
Explanation:
The default initial capacity of a LinkedHashSet is 16.
9. What is the time complexity of basic operations like add, remove, and contains in a LinkedHashSet?
Answer:
Explanation:
The basic operations in LinkedHashSet typically have a constant time complexity.
10. What advantage does LinkedHashSet have over HashSet?
Answer:
Explanation:
The main advantage of LinkedHashSet over HashSet is that it maintains the order of elements as they were inserted.
11. What method is used to get the number of elements in a LinkedHashSet?
Answer:
Explanation:
The size() method returns the number of elements in the LinkedHashSet.
12. Can LinkedHashSet be synchronized?
Answer:
Explanation:
LinkedHashSet can be synchronized by using Collections.synchronizedSet().
13. What happens when you iterate over a LinkedHashSet?
Answer:
Explanation:
When iterating over a LinkedHashSet, the elements are returned in the order they were inserted.
14. How do you clear all elements from a LinkedHashSet?
Answer:
Explanation:
The clear() method removes all elements from the LinkedHashSet.
15. What happens if you add a duplicate element to a LinkedHashSet?
Answer:
Explanation:
If a duplicate element is added to a LinkedHashSet, it is not added again, maintaining the uniqueness of elements.