Predicate and Function most useful guavas’ single abstract method Classes

This article explains, how these Predicate and Function
classes can be used. Predicate can be used to provide conditions while
transforming collection of objects to another collection.

We can use Iterables.any() to find object is exist in collection or not?.
To extend this example, we can provide condition with help of Predicate class.

boolean isHdfcBankIsExist = Iterables.any(banks, 
new Predicate() { // Predicate
            @Override
            public boolean apply(@SuppressWarnings("NullableProblems") @NotNull final Bank inBank) {
                return "HDFC".equals( inBank.getBankName()); //define condition for equality.
            }
        });

Predicate class can be used while deleting some objects from collection.

// Here hdfc & hdfc1, both objects are equal. As the bankName attribute value is equal for both.
final boolean removed = Iterables.removeIf(banks, 
new Predicate() { // Predicate
	@Override
	public boolean apply(final Bank inBank) {
		return "HDFC".equals(inBank.getBankName()); // Predicate Condition
	}
});

Function can be used while transforming one collection of objects to another.

private static List getBankNamesFromBanks(final List inBanks) {
return ImmutableList.copyOf(Iterables.transform(inBanks, 
new Function() {
@Override
public String apply(@Nullable final Bank inBank) {
return inBank.getBankName();
}
}));

Example that helps to understand how this Predicate class be used while transform only particular group of objects.
This can be done by placing simple condition using Predicate condition

Predicate example :
SimpleExampleUsingPredicate.java

import java.util.ArrayList;
import java.util.List;
import com.google.common.base.Predicate;
import com.google.common.collect.Iterables;
import org.jetbrains.annotations.NotNull;

public class SimpleExampleUsingPredicate{

    public static void main(String[] args) {

        Bank sbi = new Bank("SBI", "SBI_CODE", "SBI_IFSC", "OAK");
        Bank icici = new Bank("ICICI", "ICICI_CODE", "ICICI_IFSC", "OAK");
        Bank hdfc = new Bank("HDFC", "HDFC_CODE", "HDFC_IFSC", "CA");

        final List banks = new ArrayList();
        banks.add(sbi);
        banks.add(icici);
        banks.add(hdfc);

        System.out.println("###### Before Deleting HDFC Bank from collection #####");
        for( Bank bank : banks){
            System.out.println("Bank Name:" + bank.getBankName() );
        }

        // Here hdfc & hdfc1, both objects are equal. As the bankName attribute value is equal for both.
        boolean isHdfcBankIsExist = Iterables.any(banks, new Predicate() {
            @Override
            public boolean apply(@SuppressWarnings("NullableProblems") @NotNull final Bank inBank) {
                return "HDFC".equals( inBank.getBankName());
            }
        });

        System.out.println("\nIs HDFC Bank is existed in collection:" + isHdfcBankIsExist );

        // Here hdfc & hdfc1, both objects are equal. As the bankName attribute value is equal for both.
        final boolean removed = Iterables.removeIf(banks, new Predicate() {
            @Override
            public boolean apply(final Bank inBank) {
                return "HDFC".equals(inBank.getBankName());
            }
        });

        System.out.println("\nIs HDFC Bank is Deleted: " + removed);
        System.out.println("###### After Deleting HDFC Bank from collection #####");
        for( Bank bank : banks){
            System.out.println("Bank Name:" + bank.getBankName() );
        }

        isHdfcBankIsExist = Iterables.any(banks, new Predicate() {
            @Override
            public boolean apply(@SuppressWarnings("NullableProblems") @NotNull final Bank inBank) {
                return "HDFC".equals( inBank.getBankName());
            }
        });

        System.out.println("\nIs HDFC Bank is existed in collection:" + isHdfcBankIsExist );
    }

    public static class Bank {
        private String bankName;
        private String bankCode;
        private String ifscCode;
        private String location;

        public Bank(final String inBankName, final String inBankCode, final String inIfscCode, final String inLocation) {
            bankName = inBankName;
            bankCode = inBankCode;
            ifscCode = inIfscCode;
            location = inLocation;
        }

        public String getLocation() {
            return location;
        }

        public String getBankName() {
            return bankName;
        }
    }
}

Out Put:
###### Before Deleting HDFC Bank from collection #####
Bank Name:SBI
Bank Name:ICICI
Bank Name:HDFC

Is HDFC Bank is existed in collection:true

Is HDFC Bank is Deleted: true
###### After Deleting HDFC Bank from collection #####
Bank Name:SBI
Bank Name:ICICI

Is HDFC Bank is existed in collection:false

Function example :
FunctionExample.java

import java.util.ArrayList;
import java.util.List;
import com.google.common.base.Function;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.Iterables;
import org.jetbrains.annotations.Nullable;

public class FunctionExample {

public static void main(String[] args) {

Bank sbi = new Bank("SBI", "SBI_CODE", "SBI_IFSC");
Bank icici = new Bank("ICICI", "ICICI_CODE", "ICICI_IFSC");
Bank hdfc = new Bank("HDFC", "HDFC_CODE", "HDFC_IFSC");

List banks = new ArrayList();
banks.add(sbi);
banks.add(icici);
banks.add(hdfc);

//To get list of Bank Names from List of Banks.
List bankNames = getBankNamesFromBanks(banks);

for (String bankName : bankNames) {
System.out.println("Bank Name is: " + bankName);
}
}

private static List getBankNamesFromBanks(final List inBanks) {
return ImmutableList.copyOf(Iterables.transform(inBanks, 
new Function() { // Function
@Override
public String apply(@Nullable final Bank inBank) {
return inBank.getBankName();
}
}));
}

public static class Bank {
private String bankName;
private String bankCode;
private String ifscCode;

public Bank(final String inBankName, final String inBankCode, final String inIfscCode) {
bankName = inBankName;
bankCode = inBankCode;
ifscCode = inIfscCode;
}

public String getBankName() {
return bankName;
}
}
}

Out put:
Bank Name is: SBI
Bank Name is: ICICI
Bank Name is: HDFC

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