Skip to main content
replaced http://codereview.stackexchange.com/ with https://codereview.stackexchange.com/
Source Link

This is a follow-on qestion from: LRU cache designLRU cache design

##Revised Implementation

package lrucache;

import java.util.LinkedHashMap;


public class LRUCacheV2<K, V> extends LinkedHashMap<K, V>{
    
    private static final int MAX_ENTRIES = 3;
         /**
     * super class constructor arguments are
     * cache size = MAX_ENTRIES
     * load factor = 0.75F
     * access order = true
     */
    public LRUCacheV2(){
        super(MAX_ENTRIES,0.75F,true);
    }
    
    @Override
    protected boolean removeEldestEntry(java.util.Map.Entry<K, V> entry) {
        return size() > MAX_ENTRIES;
    }
    
    public static void main(String[] args) {
        LRUCacheV2<String, Integer> cache = new LRUCacheV2<String, Integer>();
        cache.put("abc", 1);
        cache.put("def", 2);
        cache.put("ghi", 3);
        cache.put("jkl", 4);
        cache.put("mno", 5);
        cache.put("abc", 1);
        System.out.println(cache);
    }
}

##Output

{jkl=4, mno=5, abc=1}

This is a follow-on qestion from: LRU cache design

##Revised Implementation

package lrucache;

import java.util.LinkedHashMap;


public class LRUCacheV2<K, V> extends LinkedHashMap<K, V>{
    
    private static final int MAX_ENTRIES = 3;
         /**
     * super class constructor arguments are
     * cache size = MAX_ENTRIES
     * load factor = 0.75F
     * access order = true
     */
    public LRUCacheV2(){
        super(MAX_ENTRIES,0.75F,true);
    }
    
    @Override
    protected boolean removeEldestEntry(java.util.Map.Entry<K, V> entry) {
        return size() > MAX_ENTRIES;
    }
    
    public static void main(String[] args) {
        LRUCacheV2<String, Integer> cache = new LRUCacheV2<String, Integer>();
        cache.put("abc", 1);
        cache.put("def", 2);
        cache.put("ghi", 3);
        cache.put("jkl", 4);
        cache.put("mno", 5);
        cache.put("abc", 1);
        System.out.println(cache);
    }
}

##Output

{jkl=4, mno=5, abc=1}

This is a follow-on qestion from: LRU cache design

##Revised Implementation

package lrucache;

import java.util.LinkedHashMap;


public class LRUCacheV2<K, V> extends LinkedHashMap<K, V>{
    
    private static final int MAX_ENTRIES = 3;
         /**
     * super class constructor arguments are
     * cache size = MAX_ENTRIES
     * load factor = 0.75F
     * access order = true
     */
    public LRUCacheV2(){
        super(MAX_ENTRIES,0.75F,true);
    }
    
    @Override
    protected boolean removeEldestEntry(java.util.Map.Entry<K, V> entry) {
        return size() > MAX_ENTRIES;
    }
    
    public static void main(String[] args) {
        LRUCacheV2<String, Integer> cache = new LRUCacheV2<String, Integer>();
        cache.put("abc", 1);
        cache.put("def", 2);
        cache.put("ghi", 3);
        cache.put("jkl", 4);
        cache.put("mno", 5);
        cache.put("abc", 1);
        System.out.println(cache);
    }
}

##Output

{jkl=4, mno=5, abc=1}
Add follow-on link, markdown
Source Link
rolfl
  • 98.1k
  • 17
  • 219
  • 419

working solution

This is a follow-on qestion from: LRU cache design

##Revised Implementation

package lrucache;

import java.util.LinkedHashMap;


public class LRUCacheV2<K, V> extends LinkedHashMap<K, V>{
    
    private static final int MAX_ENTRIES = 3;
         /**
     * super class constructor arguments are
     * cache size = MAX_ENTRIES
     * load factor = 0.75F
     * access order = true
     */
    public LRUCacheV2(){
        super(MAX_ENTRIES,0.75F,true);
    }
    
    @Override
    protected boolean removeEldestEntry(java.util.Map.Entry<K, V> entry) {
        return size() > MAX_ENTRIES;
    }
    
    public static void main(String[] args) {
        LRUCacheV2<String, Integer> cache = new LRUCacheV2<String, Integer>();
        cache.put("abc", 1);
        cache.put("def", 2);
        cache.put("ghi", 3);
        cache.put("jkl", 4);
        cache.put("mno", 5);
        cache.put("abc", 1);
        System.out.println(cache);
    }
}

output

{jkl=4, mno=5, abc=1} ##Output

{jkl=4, mno=5, abc=1}

working solution

package lrucache;

import java.util.LinkedHashMap;


public class LRUCacheV2<K, V> extends LinkedHashMap<K, V>{
    
    private static final int MAX_ENTRIES = 3;
         /**
     * super class constructor arguments are
     * cache size = MAX_ENTRIES
     * load factor = 0.75F
     * access order = true
     */
    public LRUCacheV2(){
        super(MAX_ENTRIES,0.75F,true);
    }
    
    @Override
    protected boolean removeEldestEntry(java.util.Map.Entry<K, V> entry) {
        return size() > MAX_ENTRIES;
    }
    
    public static void main(String[] args) {
        LRUCacheV2<String, Integer> cache = new LRUCacheV2<String, Integer>();
        cache.put("abc", 1);
        cache.put("def", 2);
        cache.put("ghi", 3);
        cache.put("jkl", 4);
        cache.put("mno", 5);
        cache.put("abc", 1);
        System.out.println(cache);
    }
}

output

{jkl=4, mno=5, abc=1}

This is a follow-on qestion from: LRU cache design

##Revised Implementation

package lrucache;

import java.util.LinkedHashMap;


public class LRUCacheV2<K, V> extends LinkedHashMap<K, V>{
    
    private static final int MAX_ENTRIES = 3;
         /**
     * super class constructor arguments are
     * cache size = MAX_ENTRIES
     * load factor = 0.75F
     * access order = true
     */
    public LRUCacheV2(){
        super(MAX_ENTRIES,0.75F,true);
    }
    
    @Override
    protected boolean removeEldestEntry(java.util.Map.Entry<K, V> entry) {
        return size() > MAX_ENTRIES;
    }
    
    public static void main(String[] args) {
        LRUCacheV2<String, Integer> cache = new LRUCacheV2<String, Integer>();
        cache.put("abc", 1);
        cache.put("def", 2);
        cache.put("ghi", 3);
        cache.put("jkl", 4);
        cache.put("mno", 5);
        cache.put("abc", 1);
        System.out.println(cache);
    }
}

##Output

{jkl=4, mno=5, abc=1}
corrected the loadfactor in constructor arg
Source Link

working solution

package lrucache;

import java.util.LinkedHashMap;


public class LRUCacheV2<K, V> extends LinkedHashMap<K, V>{
    
    private static final int MAX_ENTRIES = 3;
         /**
     * super class constructor arguments are
     * cache size = MAX_ENTRIES
     * load factor = 0.75F
     * access order = true
     */
    public LRUCacheV2(){
        super(MAX_ENTRIES,0.075F75F,true);
    }
    
    @Override
    protected boolean removeEldestEntry(java.util.Map.Entry<K, V> entry) {
        return size() > MAX_ENTRIES;
    }
    
    public static void main(String[] args) {
        LRUCacheV2<String, Integer> cache = new LRUCacheV2<String, Integer>();
        cache.put("abc", 1);
        cache.put("def", 2);
        cache.put("ghi", 3);
        cache.put("jkl", 4);
        cache.put("mno", 5);
        cache.put("abc", 1);
        System.out.println(cache);
    }
}

output

{jkl=4, mno=5, abc=1}

working solution

package lrucache;

import java.util.LinkedHashMap;


public class LRUCacheV2<K, V> extends LinkedHashMap<K, V>{
    
    private static final int MAX_ENTRIES = 3;
         /**
     * super class constructor arguments are
     * cache size = MAX_ENTRIES
     * load factor = 0.75F
     * access order = true
     */
    public LRUCacheV2(){
        super(MAX_ENTRIES,0.075F,true);
    }
    
    @Override
    protected boolean removeEldestEntry(java.util.Map.Entry<K, V> entry) {
        return size() > MAX_ENTRIES;
    }
    
    public static void main(String[] args) {
        LRUCacheV2<String, Integer> cache = new LRUCacheV2<String, Integer>();
        cache.put("abc", 1);
        cache.put("def", 2);
        cache.put("ghi", 3);
        cache.put("jkl", 4);
        cache.put("mno", 5);
        cache.put("abc", 1);
        System.out.println(cache);
    }
}

output

{jkl=4, mno=5, abc=1}

working solution

package lrucache;

import java.util.LinkedHashMap;


public class LRUCacheV2<K, V> extends LinkedHashMap<K, V>{
    
    private static final int MAX_ENTRIES = 3;
         /**
     * super class constructor arguments are
     * cache size = MAX_ENTRIES
     * load factor = 0.75F
     * access order = true
     */
    public LRUCacheV2(){
        super(MAX_ENTRIES,0.75F,true);
    }
    
    @Override
    protected boolean removeEldestEntry(java.util.Map.Entry<K, V> entry) {
        return size() > MAX_ENTRIES;
    }
    
    public static void main(String[] args) {
        LRUCacheV2<String, Integer> cache = new LRUCacheV2<String, Integer>();
        cache.put("abc", 1);
        cache.put("def", 2);
        cache.put("ghi", 3);
        cache.put("jkl", 4);
        cache.put("mno", 5);
        cache.put("abc", 1);
        System.out.println(cache);
    }
}

output

{jkl=4, mno=5, abc=1}

Source Link
Loading