FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
Java/src/main/java/com/caching/LRUCache.java at Development · iDev-git/Java · GitHub
iDev-git
Java
Repository navigation
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
Java
/
src
/
main
/
java
/
com
/
caching
/
LRUCache.java
Copy path
More file actions
More file actions
Latest commit
History
History
History
92 lines (81 loc) · 3.22 KB
Breadcrumbs
Java
/
src
/
main
/
java
/
com
/
caching
/
LRUCache.java
Copy path
File metadata and controls
92 lines (81 loc) · 3.22 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
package
com
.
caching
;
import
java
.
util
.
LinkedHashMap
;
import
java
.
util
.
Map
;
import
java
.
util
.
NoSuchElementException
;
/**
* Your LRUCache can be instantiated and called as such:
* LRUCache lruCache = new LRUCache(capacity);
* lruCache.put(key,value);
* int param_1 = lruCache.get(key);
*/
public
class
LRUCache
<
T
> {
/**
* The class LinkedHashMap,is a subclass of HashMap that preserves the insertion order.
* We can take advantage of this class to avoid having to implement the linked list.
* A special constructor is provided to create a linked hash map whose order of
* iteration is the order in which its entries were least-recently (access-order).
*/
private
final
LinkedHashMap
<
Integer
,
T
>
cache
;
private
final
int
capacity
;
/**
* @param capacity - Instantiates LRUCache with the given capacity.
*/
public
LRUCache
(
int
capacity
) {
this
.
capacity
=
capacity
;
/*
@param loadFactor Load Factor is a measure, which decides when exactly to resize the
* HashMap. By default capacity = 16 and loadFactor = 0.75f. This means
* that reisze when HashMap reaches 75% of its capacity. For we will
* remove an element only if the cache reaches 100% capacity (1.0f).
*
* @param accessOrder - Set to true if ordering mode is specified (removeEldestEntry).
*/
this
.
cache
=
new
LinkedHashMap
<
Integer
,
T
>(
capacity
,
1.0f
,
true
) {
/**
* @param eldest - The least recently accessed entry This is the entry that will
* be removed if the method returns {@code true}.
* returns {@code false} if it should be retained.
*/
@
Override
protected
boolean
removeEldestEntry
(
Map
.
Entry
eldest
) {
return
this
.
size
() >
capacity
;
}
};
}
/**
* To put a value in LRU cache with corresponding key
* We add the value for key only if the key is not present.
* We don't update existing values, only access-order is updated.
*
* @param key The key (int)
* @param value The value to be cached
*/
public
void
put
(
int
key
,
T
value
) {
if
(
capacity
==
0
) {
System
.
out
.
println
(
"Cache set to 0 capacity. No elements will be cached"
);
}
if
(!
cache
.
containsKey
(
key
)) {
cache
.
put
(
key
,
value
);
System
.
out
.
println
(
"Adding new key:"
+
key
+
" to cache"
);
}
else
{
System
.
out
.
println
(
"Key:"
+
key
+
" already present in cache. Access order will be updated."
);
}
}
/**
* To get the cached value for given key
*
* @param key The key (int) of the expected value
* @return corresponding value for input key
* @throws NoSuchElementException if key is absent
*/
public
T
get
(
int
key
) {
// cache hit condition
if
(
cache
.
containsKey
(
key
)) {
T
value
=
cache
.
get
(
key
);
System
.
out
.
println
(
"Returning value from cache:"
+
value
);
return
value
;
}
// cache miss condition
throw
new
NoSuchElementException
(
"No element found for key:"
+
key
);
}
}
Back
|
FazBrowse Home
|
New Git URL