从Collection中选取对象的一种优雅写法

Source linkhttp://stackoverflow.com/questions/587404/java-finding-objects-in-collections/588452#588452


Sample code is:

public  static  void  main(String[] args) {
            Collection<String> bigList =  new  ArrayList<String>();
            bigList.add(  "Nick" );
            bigList.add(  "Bay" );
            bigList.add(  "Lei" );
            bigList.add(  "Jonna" );

            System. out.println(bigList);


            Collection<String> smallList = CollectionUtils. select(bigList,  new  Predicate(){
                    public  boolean  evaluate(Object o) {
                  String attribute = (String)o;
                     return  attribute.equals( "Jonna" ) || attribute.equals( "Bay"  ) ;
                }
            }) ;
            
            System.  out .println(smallList);
      }


output: 
[Jonna, Bay]

你可能感兴趣的:(Collection)