Forked from jackrabb1t/CollectionsPredicateFilterSample.java
Last active
August 29, 2015 14:07
-
-
Save croucha/5df8ef552f759b67a13f to your computer and use it in GitHub Desktop.
Using predicates in apache common collections
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
import static java.lang.System.out; | |
import static java.util.Arrays.asList; | |
import java.util.ArrayList; | |
import java.util.List; | |
import org.apache.commons.collections.CollectionUtils; | |
import org.apache.commons.collections.Predicate; | |
public class ListTests { | |
public static void main( String[] args ) { | |
List<String> names = asList( "Ted", "Fred", "Jed", "Ned" ); | |
out.println( names ); | |
List<String> shortNames = new ArrayList<String>(); | |
shortNames.addAll( names ); | |
CollectionUtils.filter( shortNames, new Predicate(){ | |
public boolean evaluate( Object input ) { | |
return ((String) input).length() < 4; | |
} | |
} ); | |
out.println( shortNames.size() ); | |
for( String s : shortNames ) | |
out.println( s ); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment