Posts

Showing posts from January, 2016

Implemention of LRU Cache

Source: import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentLinkedDeque; /** * Class LRU Cache * @author Shashwat * @param <Key> * @param <Value> */ public class LRUCache<Key, Value> { private final int MAX_SIZE; private ConcurrentHashMap<Key, Value> map; private ConcurrentLinkedDeque<Key> queue; public LRUCache(final int maxSize) { this.MAX_SIZE = maxSize; this.map = new ConcurrentHashMap<Key, Value>(this.MAX_SIZE); this.queue = new ConcurrentLinkedDeque<Key>(); } public Value get(Key key) { return this.map.get(key); } public void put(final Key key, final Value value) { if (this.map.containsKey(key)) { this.queue.remove(key); } while (this.queue.size() >= this.MAX_SIZE) { Key o

Java Puzzle

Question: class Puzzle { public static void main(String[] args) { System.out.print(12345 + 5432l); System.out.print(" "); System.out.print(01234 + 43210); } } Output : 17777 43878 In lines: System.out.print(12345 + 5432l); 5432l ---- Its last character is L -- means long System.out.print(01234 + 43210); 01234 ---- Here starting digit is 0 which means octal Fixed version : class Puzzle { public static void main(String[] args) { System.out.print(12345 + 5432L); // make the last character L System.out.print(" "); System.out.print(1234 + 43210); // drop the 0 } } Output : 66666 44444