private static < T > void shrinkSet ( final Set < T > set , int newSize ) { if ( ! ( set instanceof LinkedHashSet ) ) throw new IllegalStateException ( "Cannot shrink a set which is not of type LinkedHashSet" ) ; if ( newSize > set . size ( ) ) throw new IllegalStateException ( "Cannot shrink a set of size " + set . size ( ) + " to new size " + newSize ) ; Iterator < T > entryIterator = set . iterator ( ) ; int count = 0 ; while ( count < newSize ) { entryIterator . next ( ) ; count ++ ; } while ( entryIterator . hasNext ( ) ) { entryIterator . next ( ) ; entryIterator . remove ( ) ; } }
Shrinks a given set to a given size