Google Guava Iterables Any Example

This post explains how to use Google Guava Iterables’ Any method.

Other Google Guava examples:
Convert List to Map
Iterables Find Example
Better null checks using Optional Example
Convert Map to String


package com.idodevjobs;

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 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 GoogleGuavaIterablesAnyExample(String input) {
        this.input = input;
    }

    public String getInput() {
        return input;
    }

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

	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.

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

4 Responses to Google Guava Iterables Any Example

  1. Pingback: Convert List to Map using Google Guava in Java | iDoDevJobs

  2. Pingback: Google Guava Iterables Find Example | iDoDevJobs

  3. Pingback: Google Guava Optional Example | iDoDevJobs

  4. Pingback: Convert Map to String using Google Guava in Java | iDoDevJobs

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