Search
 
SCRIPT & CODE EXAMPLE
 

JAVA

LRU Cache java

class LRUCache {

    LinkedHashMap<Integer,Integer> data = new LinkedHashMap<Integer,Integer>();
    int capacity =0;
    int value = -1;
    
    public LRUCache(int capacity) {
        this.capacity = capacity;
    }
    
    public int get(int key) {
        
        if(data.containsKey(key)){
            value = data.get(key);
            data.remove(key);
            data.put(key,value);
        }else{
            value = -1;
        }
        return value;
    }
    
    public void put(int key, int value) {
        if(!data.containsKey(key)){
            
            if(data.size() >= capacity ){
                
                for(Map.Entry first : data.entrySet()){
                    data.remove(first.getKey());
                    break;
                }
                
            }
            
            data.put(key,value);
            
        }else{
            data.remove(key);
            data.put(key,value);
        }
    }
}
Comment

PREVIOUS NEXT
Code Example
Java :: Java telegram bot dependency 
Java :: regex java 
Java :: java.lang.classcastexception: java.lang.string cannot be cast to java.util.arraylist 
Java :: java string util if empty default 
Java :: hash map java 
Java :: java static keyword 
Java :: groovy if else 
Java :: java scanner next() 
Java :: multithreading in java 
Java :: binary search algorithm java 
Java :: limit decimals java 
Java :: get image file path 
Java :: round to the next multiple of 5 
Java :: what is construct in java 
Java :: path in spring form 
Java :: how to set the length to int array in java 
Java :: java hex to rgb 
Java :: how to create a java txt file from programm 
Java :: java enum in switch statement 
Java :: matrix dimensions 
Java :: java spring mvc 
Java :: syntax for java interfaces 
Java :: deep content 
Java :: convert code from kotlin to java 
Java :: prefix vs postfix increment java 
Java :: actionlistener 
Java :: .code domain 
Java :: java get wrapper class for primitive 
Java :: java test coverage 
Java :: java scanner class time 
ADD CONTENT
Topic
Content
Source link
Name
2+3 =