Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
//License from project: LGPL 

import java.util.Collection;

import java.util.function.Predicate;

public class Main {
    /**
     * Adds the given value to the collection if the value satisfies the given predicate.
     *
     * @param <E>
     *            the collection element type
     * @param collection
     *            the collection to add the value to
     * @param value
     *            the value to add
     * @param predicate
     *            the predicate to test againts the given value
     * @return <code>true</code>, if the collection has been modified by the operation and <code>false</code> if the
     *         value was <code>null</code> or the collection already contained the value
     */
    public static <E> boolean addIf(Collection<E> collection, E value, Predicate<E> predicate) {
        if (predicate.test(value)) {
            return collection.add(value);
        }
        return false;
    }
}