Created
November 29, 2015 06:54
-
-
Save claytantor/654149bbfa2fc5b477c1 to your computer and use it in GitHub Desktop.
Filtering without Lambda.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | |
} | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public interface Filter<T> { | |
public boolean shouldRemove(T t); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I wanted a general way to filter collections without using Java 8 which is not on my servers.