Guest User

Untitled

a guest
Dec 14th, 2017
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.35 KB | None | 0 0
  1. Build Index Stage:
  2. Hashcode Design Principle:
  3. 1. one object will generate the same hash code for repeated running
  4. 2. equal -> the same hash code
  5. 3. the same hash code !-> equal but good to have (最理想的hashcode)
  6.  
  7. Default hashcode: memory address in java
  8.  
  9.  
  10.  
  11. Search according to the Index Stage:
  12. Hash Table
  13. = dictionary index
  14. Background:
  15. 1. we need o(1) to search
  16. 2. we need o(1) to add
  17.  
  18.  
  19. Collision: different elements are mapped to the same cell
  20.  
  21. Solution 1: Linear Probing
  22. Disadvantage: easy to cause clustering which reduce the search perfomance to o(k)
  23. When all cells are full: resize 2x and rehash
  24. Optimization: when cells are close to a certian capacity, we resize it
  25. Load Factor: number of entires / length <- (0,1) default by 0,75f
  26.  
  27. Solution 2: seperate chaining (java7之前中用了这个方法)
  28. Process: if the entry has an existing element, kick out it and place the new element in the place and link the kicked out element with a singel linkedlist.
  29. Problems: reduce the search performance when linked list is very long. o(k)
  30.  
  31. HashMap:
  32. Hash Code is object's memory address;
  33. How to build index: the index of array = hashcode % the length of the array
  34. Collisiton Solution: seperate chain
  35. Entry<K,V> is a key-value pair class
  36. 有必要分析HashMap的源码
  37. Optimal Modular Formula:
  38. X mod 2^n = X & (2^n - 1) (n!=0)
  39. Iteration of HashMap:
  40. 1. Obj[0] ->....->obj[n]; is not the order we insert the elements
  41. 2. have to tranverse null cell - low performance;
  42. The evolution of HashMap in Java 8:
  43. the lenght of linkedlist is very long -> the linkedlist is automatically converted to a tree -> easy to search
  44.  
  45. HashSet: is actually a special HashMap
  46. LinkedHashMap: LinkedList + HashMap (easy to traverse and traverse the order we insert elements)
  47. LinkedList: maintain the order that the elements were inserted
  48.  
  49. LRC: Least Recently Used Cache
  50. Function: maintain the access order;
  51. 如何更改LinkedHashMap保存顺序:
  52. 在构造器中把true改成false,可以实现对access order的维护
  53.  
  54. TreeMap:(one kind of Red-Black Tree)
  55. Background: for a BST:
  56. if the new element is greater than the former element every time we insert, it becomes a linked list.
  57. Solution: Red-black Tree;
  58. put():o(logn)
  59. get():o(logn)
  60. search():o(logn)
  61. Application: sort and maintain the sort feature
  62. TreeSet: one special TreeMap
  63.  
  64.  
  65. *Heap:
  66. Notion: not the heap in the JVM
  67. 1. complete tree:
  68. Every level must have maximum number of nodes(每层都满)
  69. Bottom level could be an exception(最后一层可以不满)
  70. Filled from left to right on each level(左偏)
  71. 2. Maximum/Minimum elements in the root
  72. 3. Children is smaller/larger than its parents
  73.  
  74. Heap Add/Remove:
  75. 1.Maintain the compelte tree
  76. 2.Maintain the order of parent and the child
  77.  
  78. Example remove():
  79. 1.remove the root element
  80. 2.pick the lowest and rightest element and put it in the root
  81. 3.exchange the root with its bigger child until the element cannot be exchanged any more.
  82.  
  83. Eample add():
  84. 1. add the element in the lowest and rightest position
  85. 2. exchange it with its parent util this cannot be processed any more
  86.  
  87. In the physical level:
  88. Heap is implemented by an array
  89. how to get the index of paretn and child
  90.  
  91. In Java:
  92. Heap is implemented by a Priority Queue (Priority)
  93.  
  94.  
  95. Application: get the 10 largest number of a int set
  96. 1. data set is very large -> hard to sort
  97. 2. so we have to maintain the insert order by the largest number dynamically
  98. 3. we use heap
Add Comment
Please, Sign In to add comment