-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMapLinkedHash.java
More file actions
27 lines (25 loc) · 1007 Bytes
/
MapLinkedHash.java
File metadata and controls
27 lines (25 loc) · 1007 Bytes
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
package Framework;
import java.util.LinkedHashMap;
import java.util.Map;
public class MapLinkedHash {
public static void main(String[] args) {
// LinkedHashMap<Integer, String> LHM = new LinkedHashMap<>(5,0.5f);
LinkedHashMap<Integer, String> LHM = new LinkedHashMap<>(5,0.5f,true){
// working as Cached Memory
protected boolean removeEldestEntry(Map.Entry entry){
return size()>5;
}
};
// Initial Capacity is 5 and loading function ₰ = 50%
LHM.put(1,"Apple");
LHM.put(3,"Cake");
LHM.put(2,"Donuts");
System.out.println(LHM);
LHM.put(5,"Egg");
LHM.put(4,"Bucket");
System.out.println("Value at Key=1 : "+LHM.get(1)); // Using Key=1
LHM.put(6,"Funny"); // Least Recent used is key=3 will be overridden
System.out.println("Value at Key=3 : "+LHM.get(3)); // No available
LHM.forEach((key,value)-> System.out.println(key+" -> "+value));
}
}