Java easy solution with comments


#1

public class Solution {
LinkedHashMap<Integer,Integer> map;
public Solution(int capacity) {
map=new LinkedHashMap<Integer,Integer>(capacity,0.75f,true){
//it is a function that remove the eldest entry if it exceed the size or the capacity.
protected boolean removeEldestEntry(Map.Entry eldest)
{
return size()>capacity;
}
};
}
//if the value is found,we return it or else -1
public int get(int key) {
return (map.get(key)==null)?-1:map.get(key);
}
//put the value in the map for that key
public void set(int key, int value) {
map.put(key,value);
}
}