private Set < String > extractAndRemoveUpToLimit ( Set < String > source , int limit ) { Set < String > result = new HashSet <> ( ) ; Iterator < String > iterator = source . iterator ( ) ; while ( iterator . hasNext ( ) && result . size ( ) < limit ) { result . add ( iterator . next ( ) ) ; iterator . remove ( ) ; } return result ; }
Extracts items from source in the ammount set in limit and also removes them from source