Returns an object different from the given one. - Java java.util

Java examples for java.util:Collection Element

Description

Returns an object different from the given one.

Demo Code

/*******************************************************************************
 * Copyright (c) 2010 SAP AG./*from  w w w  . j  av a  2s  . co m*/
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *    Emil Simeonov - initial API and implementation.
 *    Dimitar Donchev - initial API and implementation.
 *    Dimitar Tenev - initial API and implementation.
 *    Nevena Manova - initial API and implementation.
 *    Georgi Konstantinov - initial API and implementation.
 *    Jakob Spies - initial API and implementation.
 *******************************************************************************/
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Set;

public class Main{
    /**
     * Returns an object different from the given one.
     * @param set the set to search
     * @param obj the object to avoid
     * @param <T> type of the objects
     * @param equal equality relation to use for comparison
     * @return an object different from <code>obj</code>,
     *    if there is one, otherwise nil
     * @pre docs != null
     * @pre schema != null
     */
    public static <T> T findOther(final Collection<? extends T> set,
            final T obj, final Equal<? super T> equal) {
        Nil.checkNil(set, "docs"); //$NON-NLS-1$

        T res = null;

        for (final T doc : set) {
            if (!equal.isEqual(doc, obj)) {
                res = doc;
                break;
            }
        }

        return res;
    }
}

Related Tutorials