Created
November 18, 2011 13:47
-
-
Save jackrabb1t/1376501 to your computer and use it in GitHub Desktop.
a snippet to filter a list - using apache commons collections Predicate class
This file contains hidden or 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
Some fixes:
List<String> names = Arrays.asList( "Ted", "Fred", "Jed", "Ned" );
List<String> shortNames = new ArrayList<String>(names);