文件名称:
README.md
所在目录:
教材与参考资料 / 程序设计类 / 数据结构&算法 / 算法 / 面试与力扣题目详解 / Leetcode题目和解答 / java-leetcode / lru-cache
文件大小:
2.13 KB
下载地址:
文本预览:
## Wheel
Java do have LRU Cache in JDK,
we used to call it `LinkedHashMap`
code from JDK 1.6
```
/**
* Constructs an empty LinkedHashMap instance with the
* specified initial capacity, load factor and ordering mode.
*
* @param initialCapacity the initial capacity
* @param loadFactor the load factor
* @param accessOrder the ordering mode - true for
* access-order, false for insertion-order
* @throws IllegalArgumentException if the initial capacity is negative
* or the load factor is nonpositive
*/
public LinkedHashMap(int initialCapacity,
float loadFactor,
boolean accessOrder) {
super(initialCapacity, loadFactor);
this.accessOrder = accessOrder;
}
```
here the parameter `accessOrder` can be used to refresh newly accessed value
and
override
```
protected boolean removeEldestEntry(Map.Entry eldest) {
return false;
}
```
to remove old entry
## DIY
I'd not do this at work. but it is a good chance to learn and practice writing a `HashMap`
### HashMap
* Storing data
1. use an array Entry[] to store the data
1. `put` Entry[hash(key)] = value
1. `get` return Entry[hash(key)]
* Dealing with hash conflict
1. add next pointer to Entry in order to store key with same hash
1. `put` add to Entry.next
1. `get` search through the linked list and if equals then retun Entry.value
* Good news: no need to expand space if no enough space
### Remember the order
* Move any element to the top (`get`, `put`)
* Remove the element on the tail (`put`)
To achieve above, just threading each Entry to a doubly linked list.
## Put them together
```
Entry { key, value, hashnext, linknext, linkprev }
```
### GET(k)
```
foreach E linked list Entry[hash(k)]
if E.key == k
moveToLinkTop(E)
return E.value
not found
```
### PUT(k, v)
```
if over capacity
E = tail of doubly linked list
remove E from linked list Entry[hash(E.key)]
E = E(k,v)
add E to linked list Entry[hash(E.key)]
moveToLinkTop(E)
```
Java do have LRU Cache in JDK,
we used to call it `LinkedHashMap`
code from JDK 1.6
```
/**
* Constructs an empty LinkedHashMap instance with the
* specified initial capacity, load factor and ordering mode.
*
* @param initialCapacity the initial capacity
* @param loadFactor the load factor
* @param accessOrder the ordering mode - true for
* access-order, false for insertion-order
* @throws IllegalArgumentException if the initial capacity is negative
* or the load factor is nonpositive
*/
public LinkedHashMap(int initialCapacity,
float loadFactor,
boolean accessOrder) {
super(initialCapacity, loadFactor);
this.accessOrder = accessOrder;
}
```
here the parameter `accessOrder` can be used to refresh newly accessed value
and
override
```
protected boolean removeEldestEntry(Map.Entry
return false;
}
```
to remove old entry
## DIY
I'd not do this at work. but it is a good chance to learn and practice writing a `HashMap`
### HashMap
* Storing data
1. use an array Entry[] to store the data
1. `put` Entry[hash(key)] = value
1. `get` return Entry[hash(key)]
* Dealing with hash conflict
1. add next pointer to Entry in order to store key with same hash
1. `put` add to Entry.next
1. `get` search through the linked list and if equals then retun Entry.value
* Good news: no need to expand space if no enough space
### Remember the order
* Move any element to the top (`get`, `put`)
* Remove the element on the tail (`put`)
To achieve above, just threading each Entry to a doubly linked list.
## Put them together
```
Entry { key, value, hashnext, linknext, linkprev }
```
### GET(k)
```
foreach E linked list Entry[hash(k)]
if E.key == k
moveToLinkTop(E)
return E.value
not found
```
### PUT(k, v)
```
if over capacity
E = tail of doubly linked list
remove E from linked list Entry[hash(E.key)]
E = E(k,v)
add E to linked list Entry[hash(E.key)]
moveToLinkTop(E)
```
点赞
回复
X