Tag Archives: Core JAVA

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>() {

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

		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');
		// Get all transactions that are done online of more then 2 lakh


class Transaction{
	double amount;
	char   transactionMethod; // O for online 
	public Transaction(double amount,char transactionMethod) {
	public String toString() {
		return amount+","+transactionMethod;

Java program to Distribute N elements into Subgroup each with M element

package com.distributor;

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

public class Distributor {
public static void main(String args[]) {
int[] ar = { 1, 2, 3, 4, 5, 6 ,7,8,9};//array holding n elements n=9

int subgrplen = 3;//elements in each sub group m=3
int counter = 0;
int temp = 1;
List<Integer> lstE = new ArrayList<Integer>();
System.out.println(“the size” + lstE.size());

int[] subar = new int[3];

for (int i = 0; (i < ar.length – subgrplen + 1); i++) {
counter = 0;

for (int j = i + 1; j <= ar.length – (subgrplen – 1); j++) {
for (int k = j; counter < subgrplen; k++) {



counter = 1;
// System.out.println();