How to Filter Collections by Predicate with Guava

import static java.lang.System.out;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;


public final class GuavaSample {

  public static void main(final String[] args) {
    final List<String> list = new ArrayList<String>();



    final Iterator<String> iter = Iterators.filter(list.iterator(), new Predicate<String>() {

        public boolean apply(final String input) {
          return "Simples".equals(input);

      }); // or Collections2, Iterables

    while (iter.hasNext())
      out.println(; // only prints "Simples"



Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.