Collections Flashcards
Difference between ArrayList and Vector?
1) Vector are Synchronized, due for deprecated, slow, poorly designed, not recommended to use
2)ArrayList are not thread safe
3) thread safe of ArrayList
CopyOnWriteArrayList
List namesList = Collections.synchronizedList(new ArrayList());
Character of ArrayList ?
1) Backed by a dynamic array
2) random index access is O(1)
3) manipulation is slow compare with linked list, when one element added/removed, need to move all impacted bits in array
Character of LinkedList?
1) internally uses a doubly linked list to store the elements
2) No ramdom index access, need O(n) to find the element
3) Manipulation with LinkedList is faster than ArrayList because it uses a doubly linked list, so no bit shifting is required in memory.
4) Can be used as a Queue/Stack because it implements Deque interfaces
5) have memory over head as need to store the pointers
How HashMap internally works?
- Java7: Array + Linkedlist
Java8: Array+LinkedList+RedBlackTree(when linkedlist size >8)
Entry: key, value, hashcode, next pointer
V never used in comparation!
2.
Capacity - array size, doubled when extended
LoadFactor - default 0.75
threshold - of Extend, = capacity*loadFactor - if not will cause hash collision degreed the performance
- how get(key)/put(k, v) works
call key.hashCode(), find the bucket
call key.equals(otherKey), find the elements
How HashSet internally works?
HashMap with all Node with dummy V.
Array’s HashCode - need tests
1) Since arrays don’t override the Object.hashCode method. Changing the array’s contents thus does not affect its hash code.
2) To get an array’s hash code correctly, you should call Arrays.hashCode(nums).
HashSet and TreeSet Difference?
- HSimplemented by HashTable, TS-> Tree structure
- HS elements are unordered, TS elements are sorted
- HS add/remove/contans is O(1), TS is O(logN)
- HS using equals() check duplication, TS using compareTo()
- TS not allow null elements
- 2 elements equal() must , compareTo() should return 0
LinkedHashMap internal implementation?
- HashMap with gauranteed order of insertion
2. Using a DoubleLinkedList to keep the insertion order
Interfaces on Collection framework?
1. Collection List->(ArrayList, LinkedList, Vector->Stack) Set->(HashSet, LinkedHashSet) ->SortedSet->NavigableSet->(TreeSet) Queue->Deque->(LinkedList)
- Map->(HashMap, LinkedHashMap)
- >SortedMap->NavigableMap->(TreeMap)
3.Iterator->ListIterator
Fail Fast Iterator and Fail safe Iterator
Fail-safe iterators - will not throw any exception even if the collection is modified while iterating over it.
Fail-fast iterators throw an exception(ConcurrentModificationException) if the collection is modified while iterating over it.
Iterator Vs ListIterator
Iterator
- can be used on Map, set ,list
- can only traverse forward
- can not modify elements
- can not return index
ListIterator
- can only used on List
- can traverse forward and backword: next(), previous()
- can modify elements add(), remove(), set()
- can return index nextIndex(), previousIndex()
Collection Vs Collections
Collection - super Interface of List, Set
Collections - Utility class for Collection - because previous java8 version does not allow default methods
addAll(other)
emptyList/Map/Set() - parameterized type safe
EMPTY_SET/LIST/MAP - return raw type
reverse(List> list)
max/min(Collections c), max/min(Collections c, comparator c)
Coding 1. Implement HashMap using array
- entryclass
2. mymap class, put, get
Coding 2. Implement ArrayList using array
todo