public static Set < String > retainMatchingKeys ( Counter < String > counter , List < Pattern > matchPatterns ) { Set < String > removed = new HashSet < String > ( ) ; for ( String key : counter . keySet ( ) ) { boolean matched = false ; for ( Pattern pattern : matchPatterns ) { if ( pattern . matcher ( key ) . matches ( ) ) { matched = true ; break ; } } if ( ! matched ) { removed . add ( key ) ; } } for ( String key : removed ) { counter . remove ( key ) ; } return removed ; }
Removes all entries with keys that does not match one of the given patterns