code: public void trimToSize(int maxSize) {
          while (true) {
              K key;
              V value;
              synchronized (this) {
                  if (size < 0 || (map.isEmpty() && size != 0)) {
                      throw new IllegalStateException(getClass().getName()
                              + ".sizeOf() is reporting inconsistent results!");
                  }
  
                  if (size <= maxSize) {
                      break;
                  }
                  Map.Entry<K, V> toEvict = null;
                  try {
                      toEvict = (Map.Entry<K, V>) map.getClass().getMethod("eldest").invoke(map);
  
                  } catch (NoSuchMethodException e) {
                      e.printStackTrace();
                  } catch (InvocationTargetException e) {
                      e.printStackTrace();
                  } catch (IllegalAccessException e) {
                      e.printStackTrace();
                  }
                  if (toEvict == null) {
                      break;
                  }
                  key = toEvict.getKey();
                  value = toEvict.getValue();
                  map.remove(key);
                  size -= safeSizeOf(key, value);
                  evictionCount++;
              }
              entryRemoved(true, key, value, null);
          }
      }
docs: Remove the eldest entries until the total of remaining entries is at or
  below the requested size.
  
  @param maxSize the maximum size of the cache before returning. May be -1
  to evict even 0-sized elements.