Govt Exams
ListIterator extends Iterator and provides bidirectional traversal using previous() and next() methods.
CopyOnWriteArrayList maintains insertion order and is thread-safe. It creates a copy of the array on modification.
ArrayList.add() has O(1) amortized time complexity. When capacity is exceeded, resizing takes O(n).
removeIf(Predicate) removes all elements that satisfy the given predicate condition (Java 8+).
LinkedList provides O(1) insertions/deletions at any position. ArrayList is O(n) for middle insertions due to shifting.
Fail-fast iterators throw ConcurrentModificationException when collection is modified during iteration.
HashSet uses hash table internally. Average case is O(1), worst case O(n) when hash collisions occur.
List list = Arrays.asList("A", "B", "C");
list.add("D");
System.out.println(list.size());
Arrays.asList() returns a fixed-size list backed by array. add() operation is not supported.
Collection col = new ArrayList();
col.add("Java");
Iterator it = col.iterator();
while(it.hasNext()) {
String s = it.next();
col.remove(s);
}
Modifying collection directly while iterating throws ConcurrentModificationException. Use iterator.remove() instead.
PriorityQueue pq = new PriorityQueue();
pq.add(5);
pq.add(3);
pq.add(7);
System.out.println(pq.peek());
PriorityQueue orders elements based on their natural ordering (min-heap by default). peek() returns 3, the minimum element.