Create LRUcache
LRU algorithm class:
import java.util.LinkedHashMap; import java.util.Map; public class LRUAlgoritm<K, V> implements Cache<K, V> { private final LRUStorage storage;//Π½Π΅ Ρ
ΠΎΡΠ΅Ρ ΠΊΠΎΠΌΠΏΠΈΠ»ΠΈΡΡΡ Ρ.ΠΊ."The blank final //field storage may not have been initialized" // ΠΏΠΎΠΌΠΎΠ³ΠΈΡΠ΅ ΠΊΠ°ΠΊ Π·Π΄Π΅ΡΡ ΠΏΡΠ°Π²ΠΈΠ»ΡΠ½ΠΎ ΡΠ΅Π°Π»ΠΈΠ·ΠΎΠ²Π°ΡΡ @Override public V get(K key) { return storage.get(key); } @Override public V put(K key, V value) { return storage.put(key, value); } private class LRUStorage extends LinkedHashMap<K, V> { private final int capacity; private LRUStorage(int capacity) { this.capacity = capacity; } protected boolean removedEldestEntry(Map.Entry<K, V> eldest) { return size() > capacity; } } } Cache interface is simple:
public interface Cache <K,V>{ V get (K key); V put (K key, V value); } in runner class
public class Runner { public static void main (String[] args){ LRUAlgoritm<String, String> lruAlgoritm = new LRUAlgoritm<>(); lruAlgoritm.put("1","1"); } } trying to create cache overload and can't
- The compiler requires the
LRUalgoritmclassLRUalgoritminitialize thestorageconstant. How does it need to be initialized here? - How to apply to the object of class
LRUStorage, that would set my capacity