Files
java-interview-lab/fresher-qa/output/04-collections.txt
T

50 lines
2.5 KiB
Plaintext

=== Q28 The Collections Framework hierarchy ===
ArrayList extends AbstractList implements [List, RandomAccess, Cloneable, Serializable]
LinkedList extends AbstractSequentialList implements [List, Deque, Cloneable, Serializable]
HashSet extends AbstractSet implements [Set, Cloneable, Serializable]
TreeSet extends AbstractSet implements [NavigableSet, Cloneable, Serializable]
ArrayDeque extends AbstractCollection implements [Deque, Cloneable, Serializable]
HashMap extends AbstractMap implements [Map, Cloneable, Serializable]
TreeMap extends AbstractMap implements [NavigableMap, Cloneable, Serializable]
=== Q29 ArrayList vs LinkedList ===
ArrayList is RandomAccess: true
LinkedList is RandomAccess: false
LinkedList is also a Deque: true
after add(0, 5): [5, 10, 20, 30], get(2) = 20
=== Q30 How HashMap works internally ===
Aa: hashCode=2112, bucket (16 slots)=0
BB: hashCode=2112, bucket (16 slots)=0
Ankur: hashCode=63412443, bucket (16 slots)=12
both survive a collision: first, second, size 2
=== Q31 HashMap vs Hashtable vs ConcurrentHashMap vs TreeMap ===
HashMap: null key accepted
Hashtable: null key throws NullPointerException
ConcurrentHashMap: null key throws NullPointerException
TreeMap: null key throws NullPointerException
=== Q32 HashSet vs LinkedHashSet vs TreeSet ===
HashSet [banana, apple, cherry, pear, mango]
LinkedHashSet [pear, apple, mango, banana, cherry]
TreeSet [apple, banana, cherry, mango, pear]
=== Q33 Comparable vs Comparator ===
natural (Comparable): [Student[name=Asha, marks=88], Student[name=Meena, marks=88], Student[name=Ravi, marks=90]]
marks desc, then name: [Student[name=Ravi, marks=90], Student[name=Asha, marks=88], Student[name=Meena, marks=88]]
=== Q34 Fail-fast iterators and ConcurrentModificationException ===
removing 2 inside for-each -> ConcurrentModificationException
removing the second-to-last element threw nothing, list = [1, 2, 4]
removeIf even -> [1, 3, 5]
=== Q35 Arrays.asList() and List.of() pitfalls ===
write-through to the array: changed
Arrays.asList().add -> UnsupportedOperationException
Arrays.asList(int[]) has 1 element
List.of(.., null) -> NullPointerException
=== Q36 Queue, Deque and PriorityQueue ===
PriorityQueue polls: 1 2 3 4 5
ArrayDeque as a stack pops: cba
=== Q37 Mutable keys and the equals()/hashCode() contract in a HashMap ===
get with the mutated key: null
get with a fresh "A1" key: null
size is still 1
=== Q38 unmodifiableList() view vs List.copyOf() snapshot ===
view sees the change: [a, b, c]
copy does not: [a, b]