diff --git a/2025/01/cracking_code_interview_16_25_lru_cache/src/main/java/LruCache.java b/2025/01/cracking_code_interview_16_25_lru_cache/src/main/java/LruCache.java index 6b2efc7..297ed5b 100644 --- a/2025/01/cracking_code_interview_16_25_lru_cache/src/main/java/LruCache.java +++ b/2025/01/cracking_code_interview_16_25_lru_cache/src/main/java/LruCache.java @@ -1,9 +1,9 @@ import java.util.*; public class LruCache implements Map { - private Map map; - private PriorityQueue priorityQueue; - private Map counts; + private final Map map; + private final PriorityQueue priorityQueue; + private final Map counts; private final int maxSize; public LruCache(int maxSize) { @@ -34,9 +34,9 @@ public class LruCache implements Map { @Override public V get(Object key) { - counts.put(key, counts.getOrDefault(key, 0) + 1); + counts.put((K) key, counts.getOrDefault(key, 0) + 1); priorityQueue.remove(key); - priorityQueue.add(key); + priorityQueue.add((K) key); return map.get(key); } diff --git a/2025/01/cracking_code_interview_16_25_lru_cache/src/main/java/Program.java b/2025/01/cracking_code_interview_16_25_lru_cache/src/main/java/Program.java index c512665..d82de8c 100644 --- a/2025/01/cracking_code_interview_16_25_lru_cache/src/main/java/Program.java +++ b/2025/01/cracking_code_interview_16_25_lru_cache/src/main/java/Program.java @@ -1,20 +1,24 @@ +import java.io.PrintStream; + public class Program { public static void main(String[] args) { + testLruCache(System.out); + } + + private static void testLruCache(PrintStream ps) { var lruCache = new LruCache<>(2); lruCache.put(1, "one"); lruCache.put(2, "two"); - - System.out.println("Element: " + lruCache.get(1)); - System.out.println("Element: " + lruCache.get(1)); - System.out.println("Element: " + lruCache.get(1)); + ps.println("Element: " + lruCache.get(1)); + ps.println("Element: " + lruCache.get(1)); + ps.println("Element: " + lruCache.get(1)); lruCache.put(3, "three"); - System.out.println("Element: " + lruCache.get(2)); + ps.println("Element: " + lruCache.get(2)); lruCache.put(4, "four"); - System.out.println("Element: " + lruCache.get(4)); - System.out.println("Element: " + lruCache.get(3)); - System.out.println("Element: " + lruCache.get(2)); - System.out.println("Element: " + lruCache.get(1)); - + ps.println("Element: " + lruCache.get(4)); + ps.println("Element: " + lruCache.get(3)); + ps.println("Element: " + lruCache.get(2)); + ps.println("Element: " + lruCache.get(1)); } }