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 || map.isEmpty()) {
                    break;
                }

                Map.Entry<K, V> toEvict = map.entrySet().iterator().next();
                key = toEvict.getKey();
                value = toEvict.getValue();
                map.remove(key);
                size -= safeSizeOf(key, value);
                evictionCount++;
            }

            entryRemoved(true, key, value, null);
        }
    }
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 || map . isEmpty ( ) ) { break ; } Map . Entry < K , V > toEvict = map . entrySet ( ) . iterator ( ) . next ( ) ; key = toEvict . getKey ( ) ; value = toEvict . getValue ( ) ; map . remove ( key ) ; size -= safeSizeOf ( key , value ) ; evictionCount ++ ; } entryRemoved ( true , key , value , null ) ; } }
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.