Linear Structure
Array: the length cannot changed
ArrayList:
当到达max length时候需要进行resize:1.5x new ArrayList & copy the old one
Add Last: Armortized O(1)
Add First: O(n)
Remove First: O(n)
Remove Last: O(1)
ArrayDeque:
当到达max的时候resize是2x原来的length
Add Last: O(1)
Add First: O(1)
Remove First: O(1)
Remove Last: O(1)
LinkedList
Dynamic data structure
Add First O(1)
Add Last O(1)
Access(随机访问) O(n)
Set O(n)
Space也是其中一个缺点
Deque
Deque<Integer> stack = new ArrayDeque<>();
Deque<Integer> queue = new ArrayDeque<>();
Hash
build index
HashCode
Many to One (多对一的情况) 完美情况 一对一
设计原则: 1. same code repeating called same application
2. equal => Same hash code
3. Same hash code != Equals, but good to have
HashTable
Use hashcode to find the index of array
Collision:
Linear Probing & Seperate Chain
Rehashing after 2/3 size of HashTable
HashMap
Hard to traver the hashmap
Seperate Chain HahTable implementation
在Java8中,List特别长的时候就会变成Tree
LinkedHashMap
LinkedList+HashMap
维护当前的HashMap顺序
HashSet
Wrapper class of the HashMap.
Value is a constant dummy object
TreeMap
永远是一个平衡树
Red-Black Tree Implementation
O(logn): put, containsKey, remove
Iteration in Order, sorted key
Heap
Complete Tree
Largest/Smallest in the root
Children always small or higher than root
Java没有Heap这个概念但是有一个数据结构PriorityQueue