/java/

Google Guava libraries example Collections filter

2013-04-13 19:33:01

https://code.google.com/p/guava-libraries/ Predicate

Predicate

<post> isStatusTrue = new Predicate<post>() {
    @Override
    public boolean apply(Post post) {
        return post.isStat();
    }
};

test

Collection<post> posts = postService.getAllPosts();
Collection<post> filterPosts = Collections2.filter(posts, isStatusTrue);

other

public class PostService {

    public Collection<post> getAllPosts(){
        Collection<post> posts = new ArrayList<post>();
        posts.add( new Post("elorem1", true) );
        posts.add( new Post("alorem1", false) );
        posts.add( new Post("hlorem1", true) );
        posts.add( new Post("dlorem1", false) );
        posts.add( new Post("glorem1", true) );
        posts.add( new Post("dlorem1", false) );
        posts.add( new Post("florem1", true) );
        posts.add( new Post("clorem1", false) );

        return posts;
    }
}
public class Post {

    private String name;
    private boolean stat;

    public Post(String name, boolean stat){
        this.name = name;
        this.stat = stat;
    }

   //get and set

}