Skip to content

Instantly share code, notes, and snippets.

@claytantor
Created November 29, 2015 06:54
Show Gist options
  • Save claytantor/654149bbfa2fc5b477c1 to your computer and use it in GitHub Desktop.
Save claytantor/654149bbfa2fc5b477c1 to your computer and use it in GitHub Desktop.
Filtering without Lambda.
public class CollectionsUtils {
public static void remove(List<?> items, Filter<Object> filter) {
Iterator<?> iterator = items.iterator();
while (iterator.hasNext()) {
if (filter.shouldRemove(iterator.next())) {
iterator.remove();
}
}
}
}
public interface Filter<T> {
public boolean shouldRemove(T t);
}
List<Map.Entry<String, Object>> list = new ArrayList<Map.Entry<String, Object>>(entries);
//remove negative values
CollectionsUtils.remove(list, new Filter<Object>() {
public boolean shouldRemove(Object o1) {
Map.Entry<String, Object> entry = (Map.Entry<String, Object>)o1;
Double d1 = Double.parseDouble(entry.getValue().toString());
return d1<0.25;
}
});
@claytantor
Copy link
Author

I wanted a general way to filter collections without using Java 8 which is not on my servers.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment