LinkedHashSet:
- A LinkedHashSet is a class in Java that:
- Implements the Set interface
- It is part of java.util package.
Key Features:
- Unique Elements -> Like all sets, it does not allow duplicates.
- Maintains Insertion Order -> Unlike HashSet (which is unordered), LinkedHashSet keeps elements in the same order you inserted them.
- Allows null -> Can store one null element.
- Performance -> Almost same as HashSet (O(1) average time for add, remove, contains).
- Slightly slower than HashSet because it also maintains a linked list for order.
Methods in LinkedHashSet:
- Since it extends HashSet, it has the same methods:
- add(E e) – add element
- remove(Object o) – remove element
- contains(Object o) – check if present
- size() – number of elements
- clear() – remove all elements
- isEmpty() – check if empty
Queue:
- Queue is a collection interface used to hold elements before processing, typically in FIFO (First-In-First-Out) order.
- It is part of the Java Collections Framework and defined in:
- java.util.Queue
Key Points:
- Interface → Queue is an interface, not a class.
- Order → Elements are usually processed in FIFO order, though some implementations (like PriorityQueue) order them differently.
- Duplicates → Allowed.
- Nulls → Some implementations allow one null (like LinkedList), but PriorityQueue does not.
Common Queue Methods:
(Specializes methods of Collection)
- boolean add(E e) Inserts element, throws exception if full
- boolean offer(E e) Inserts element, returns false if full
- E remove() Removes head, throws exception if empty
- E poll() Removes head, returns null if empty
- E element() Retrieves head, throws exception if empty
- E peek() Retrieves head, returns null if empty
Top comments (0)