Skip to content

Instantly share code, notes, and snippets.

@jackrabb1t
Created November 18, 2011 13:47
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save jackrabb1t/1376501 to your computer and use it in GitHub Desktop.
Save jackrabb1t/1376501 to your computer and use it in GitHub Desktop.
a snippet to filter a list - using apache commons collections Predicate class
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 );
}
}
@srayhunter
Copy link

Some fixes:

List<String> names = Arrays.asList( "Ted", "Fred", "Jed", "Ned" );
List<String> shortNames = new ArrayList<String>(names);

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