Tag Archives: JAVA8

Understanding Predicates with JAVA8

In mathematics Predicates are functions that can be either True or False. In JAVA8 Predicates are functional interfaces with only functional method test.
As Predicate is defined as a functional interface in JAVA8 it can be used as the assignment target for a lambda expression or method reference.
we can do boolean operations such as and, or, not(negate) with different instances of Predicate. These default methods are –

Default Method Name Explanation
and() It does logical AND of the predicate on which it is called with another predicate. Example: predicate1.and(predicate2)
or() It does logical OR of the predicate on which it is called with another predicate. Example: predicate1.or(predicate2)
negate() It does boolean negation of the predicate on which it is invoked. Example: predicate1.negate()

Following code uses JAVA8 predicate and replaceIf method, now available in collections, to check from the list of transaction to get only those transaction that has a value more than 2lakh and are done online.

public class UnderstandingRemoveIf {

	static Predicate<Transaction> checkTransactionAmount = new Predicate<Transaction>() {

		@Override
		public boolean test(Transaction transaction) {
			return Math.round(transaction.amount) < 200000;
		}
		
	};
	
	static Predicate<Transaction> isTransactionOnline = new Predicate<Transaction>() {

		@Override
		public boolean test(Transaction transaction) {
			return transaction.transactionMethod!='O';
		}
	};

	public static void main(String[] args) {

		List<Transaction> transactions = new ArrayList<>();
		Transaction transaction1=new Transaction(10000.8d, 'C');
		Transaction transaction2=new Transaction(150000d, 'O');
		Transaction transaction3=new Transaction(300000d, 'O');
		transactions.add(transaction1);
		transactions.add(transaction2);
		transactions.add(transaction3);
		
		// Get all transactions that are done online of more then 2 lakh
		transactions.removeIf(checkTransactionAmount.or(isTransactionOnline));
		System.out.println(transactions);
	}

}

class Transaction{
	
	double amount;
	char   transactionMethod; // O for online 
	public Transaction(double amount,char transactionMethod) {
		this.amount=amount;
		this.transactionMethod=transactionMethod;
	}
	
	@Override
	public String toString() {
		return amount+","+transactionMethod;
	}
}

Lambda Expressions JAVA 8

To understand the concept of lambda expression we need to have a clear understanding of what is a Functional Interface
An interface which declares only one abstract method is called as Functional Interface and lambda expressions let us provide the implementation of the abstract method of a Functional Interface


interface Finterface{
public abstract void functionalInterfaceMethod();
}

Now we can have the implementation of this functional interface either by using anonymous inner classes or by using the Lambda expressions
Using Anonymous inner class

Finterface finterface=new Finterface(){
public void functionalInterfaceMethod(){
System.out.println("Example lambda!");
}
};

or using Lambda expression

Finterface finterface=()->System.out.println("Example Lambda!");

()->System.out.println(“Example Lambda!”);–> provide the implementation of Finterface.

The signature of the abstract method of a functional interface describes the signature of lambda expression called as Functional Descriptor as in the above example the functionalInterfaceMethod declares that is doesn’t accepts any parameter and returns void
the same is the description of its Lambda
()# Zero parameters
-># separates the parameter with expression
System.out.println()# Expression returning void