Google Guava Iterables Any Example



import java.util.List;

import com.google.common.base.Predicate;
import com.google.common.collect.Iterables;
import com.google.common.collect.Lists;

public class GoogleGuavaIterablesAnyExample {
    private String input;

    public GoogleGuavaIterablesAnyExample(String input) {
        this.input = input;
    }

    public String getInput() {
        return input;
    }

    public void setInput(String input) {
        this.input = input;
    }

    public static void main(String[] args) {
        List<GoogleGuavaIterablesAnyExample> testList = Lists.newArrayList(
                                                                new GoogleGuavaIterablesAnyExample("input1"),
                                                                new GoogleGuavaIterablesAnyExample("input2"),
                                                                new GoogleGuavaIterablesAnyExample("input1"),
                                                                new GoogleGuavaIterablesAnyExample("input2"),
                                                                new GoogleGuavaIterablesAnyExample("input3"));
        System.out.println("isPresentUsingGuava : " + isPresentUsingGuava("input1", testList));
        System.out.println("isPresentUsingForLoop : " + isPresentUsingForLoop("input4", testList));

    }

    public static boolean isPresentUsingGuava (final String input, List<GoogleGuavaIterablesAnyExample> testList) {
        return Iterables.any(testList, new Predicate<GoogleGuavaIterablesAnyExample>() {
            public boolean apply(GoogleGuavaIterablesAnyExample googleGuavaIterablesExample) {
                return googleGuavaIterablesExample.getInput().equals(input);
            }
        });
    }
    
    public static boolean isPresentUsingForLoop (final String input, List<GoogleGuavaIterablesAnyExample> testList) {
        for (GoogleGuavaIterablesAnyExample googleGuavaIterablesAnyExample : testList) {
            if (googleGuavaIterablesAnyExample.getInput().equals(input)) {
                return true;
            }
        }
        return false;
    }

    public String toString() {
        return "GoogleGuavaIterablesExample{" + "input='" + input + '\'' + '}';
    }
}

Google Guava Vs For Loop.

In this example, I have used google guava’s iterables -> any() method to check if the collection has any object that satisfies the criteria.

This is a better approach compared to regular for loop or iterator.

About these ads
This entry was posted in java and tagged , , , , , , . Bookmark the permalink.

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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 )

Google+ photo

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

Connecting to %s