Skip to content

Instantly share code, notes, and snippets.

@tobyclemson
Created February 22, 2011 14:40
Show Gist options
  • Save tobyclemson/838759 to your computer and use it in GitHub Desktop.
Save tobyclemson/838759 to your computer and use it in GitHub Desktop.
A thing of beauty!
private <S, T extends S> void replace(
List<S> elements,
Class<T> classOfElementsToRemove,
Class<? extends T> classOfElementToAdd) {
elements.removeAll(toList(filterByType(elements, classOfElementsToRemove)));
try {
elements.add(classOfElementToAdd.newInstance());
} catch (InstantiationException exception) {
throw new RuntimeException(exception);
} catch (IllegalAccessException exception) {
throw new RuntimeException(exception);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment