Govt. Exams
Entrance Exams
LinkedHashMap can be extended and removeEldestEntry() overridden to implement LRU cache eviction policy.
Collections.sort() uses TimSort (hybrid of merge and insertion sort) with O(n log n) worst-case, but QuickSort's worst-case is O(n²).
forEachOrdered() preserves encounter order even in parallel streams, while forEach() doesn't guarantee order.
subList() returns a view (structural reference) to the original list in O(1) time, not a copy.
ConcurrentHashMap uses segment-based locking allowing safe iteration during concurrent modifications. HashMap throws ConcurrentModificationException.
Records (Java 14+) provide compact syntax for data carriers used with collections, improving performance and readability.
CopyOnWriteArrayList is optimal for read-heavy concurrent operations. It creates copy on write, allowing safe concurrent reads.
List list = Arrays.asList(5, 2, 8, 1);
All three approaches work correctly for reverse sorting. Options A, B, and C are all valid Java code.
LinkedHashMap with accessOrder=true maintains access-order. get() moves the element to the end (LRU cache behavior).
In worst case with all collisions stored in linked list, get() becomes O(n). From Java 8, if collisions exceed threshold, linked list converts to balanced tree, making it O(log n).