title: Get the top 3 elements from the memory cache
body: <p>When I need to get the <code>top 3</code> items from a <code>Map</code>, I can write the code, </p>
  
  <pre><code>private static Map&lt;String, Integer&gt; SortMapBasedOnValues(Map&lt;String, Integer&gt; map, int n) {
  
          Map&lt;String, Integer&gt; sortedDecreasingly = map.entrySet().stream()
                  .sorted(Collections.reverseOrder(Map.Entry.comparingByValue())).limit(n)
                  .collect(toMap(Map.Entry::getKey, Map.Entry::getValue, (e1, e2) -&gt; e2, LinkedHashMap::new));
  
          return sortedDecreasingly;
      }
  </code></pre>
  
  <p>I have a memory cache that I use to keep track of some app data, </p>
  
  <pre><code>public class MemoryCache&lt;K, T&gt; {
  
  
      private long timeToLive;
      private LRUMap map;
  
      protected class CacheObject {
  
          public long lastAccessed = System.currentTimeMillis();
          public T value;
  
          protected CacheObject(T value) {
              this.value = value;
          }
      }
  
      public MemoryCache(long timeToLive, final long timerInterval, int maxItems) {
  
          this.timeToLive = timeToLive * 1000;
  
          map = new LRUMap(maxItems);
  
          if (this.timeToLive &gt; 0 &amp;&amp; timerInterval &gt; 0) {
  
              Thread t = new Thread(new Runnable() {
  
                  public void run() {
                      while (true) {
                          try {
                              Thread.sleep(timerInterval * 1000);
                          } catch (InterruptedException ex) {
                          }
                          cleanup();
                      }
                  }
              });
  
              t.setDaemon(true);
              t.start();
          }
      }
  
      public void put(K key, T value) {
          synchronized (map) {
              map.put(key, new CacheObject(value));
          }
      }
  
      @SuppressWarnings("unchecked")
      public T get(K key) {
  
          synchronized (map) {
  
              CacheObject c = (CacheObject) map.get(key);
  
              if (c == null)
                  return null;
              else {
                  c.lastAccessed = System.currentTimeMillis();
                  return c.value;
              }
          }
      }
  
      public void remove(K key) {
          synchronized (map) {
              map.remove(key);
          }
      }
  
      public int size() {
          synchronized (map) {
              return map.size();
          }
      }
  
      @SuppressWarnings("unchecked")
      public void cleanup() {
  
          long now = System.currentTimeMillis();
          ArrayList&lt;K&gt; deleteKey = null;
  
          synchronized (map) {
              MapIterator itr = map.mapIterator();
  
              deleteKey = new ArrayList&lt;K&gt;((map.size() / 2) + 1);
              K key = null;
              CacheObject c = null;
  
              while (itr.hasNext()) {
                  key = (K) itr.next();
                  c = (CacheObject) itr.getValue();
  
                  if (c != null &amp;&amp; (now &gt; (timeToLive + c.lastAccessed))) {
                      deleteKey.add(key);
                  }
              }
          }
  
          for (K key : deleteKey) {
              synchronized (map) {
                  map.remove(key);
              }
  
              Thread.yield();
          }
      }
  
  }
  </code></pre>
  
  <p>Inside the app, I initialize it, </p>
  
  <pre><code>MemoryCache&lt;String, Integer&gt; cache = new MemoryCache&lt;String, Integer&gt;(200, 500, 100);
  </code></pre>
  
  <p>Then I can add the data, </p>
  
  <pre><code>cache.put("productId", 500);
  </code></pre>
  
  <p>I would like to add functionality in the <code>MemoryCache</code> class so if called will return a <code>HashMap</code> of the top 3 items based on the value. </p>
  
  <p>Do you have any advise how to implement that? </p>