Example usage for java.util Collection contains

List of usage examples for java.util Collection contains

Introduction

In this page you can find the example usage for java.util Collection contains.

Prototype

boolean contains(Object o);

Source Link

Document

Returns true if this collection contains the specified element.

Usage

From source file:Main.java

/**
 * The resultant set is those elements in superSet which are not in the subSet
 *//* w w w .java 2 s. c o m*/
public static <T> List<T> setComplement(Collection<T> superSet, Collection<T> subList) {
    ArrayList<T> complement = new ArrayList<T>(superSet.size());
    for (T obj : superSet) {
        if (!subList.contains(obj)) {
            complement.add(obj);
        }
    }
    return complement;
}

From source file:Main.java

/**
 * returns a List of union objects, i.e. objects that are present in both collection
 *
 * @param <T>// ww w  .j  av  a2s . com
 * @param initial
 * @param other
 * @return
 */
public static <T> List<T> union(Collection<T> initial, Collection<T> other) {
    ArrayList<T> union = new ArrayList<T>();

    for (T obj : initial) {

        if (other.contains(obj))
            union.add(obj);
    }

    return union;
}

From source file:Main.java

/**
 * @return The intersection of two sets A and B is the set of elements common to A and B
 *//*from   w ww.  j av a  2 s  . co  m*/
public static <T> List<T> setIntersection(Collection<T> listA, Collection<T> listB) {
    ArrayList<T> intersection = new ArrayList<T>(listA.size());

    for (T obj : listA) {
        if (listB.contains(obj)) {
            intersection.add(obj);
        }
    }
    return intersection;
}

From source file:Main.java

public static <T> boolean collectionEquals(Collection<T> col1, Collection<T> col2) {
    if (col1.size() != col2.size()) {
        return false;
    }//from ww  w. ja va 2  s  .  c om

    for (T item : col1) {
        if (!col2.contains(item)) {
            return false;
        }
    }

    return true;
}

From source file:Main.java

/**
 * Adds all non-null objects into the specified list.
 *
 * @param collection/*  ww  w  .j a va2  s.com*/
 *            list to fill
 * @param objects
 *            objects
 * @param <T>
 *            objects type
 * @return true if list changed as the result of this operation, false
 *         otherwise
 */
public static <T> boolean addAllNonNull(final Collection<T> collection, final T... objects) {
    boolean result = false;
    for (final T object : objects) {
        if (!collection.contains(object) && object != null) {
            result |= collection.add(object);
        }
    }
    return result;
}

From source file:Main.java

private static <T> Set<T> findGroupOneDelta(Collection<T> groupOne, Collection<T> groupTwo) {

    Set<T> delta = new HashSet<T>();
    for (T groupOneItem : groupOne)
        if (!groupTwo.contains(groupOneItem))
            delta.add(groupOneItem);/*from   www . ja  v  a  2  s. c  om*/
    return delta;
}

From source file:Main.java

private static <T> Set<T> findGroupTwoDelta(Collection<T> groupOne, Collection<T> groupTwo) {

    Set<T> delta = new HashSet<T>();
    for (T groupOneItem : groupOne)
        if (!groupTwo.contains(groupOneItem))
            delta.add(groupOneItem);//from  www .  j  a  va 2  s  .co m
    return delta;
}

From source file:com.marklogic.samplestack.domain.ClientRole.java

/**
 * Provides the database client role implied by the security context for the spring application.
 * @return The ClientRole enum value that corresponds to the current logged-in user.
 *///  w  ww  .  j a v  a 2  s .  c  om
public static ClientRole securityContextRole() {
    SecurityContext secContext = SecurityContextHolder.getContext();
    Collection<? extends GrantedAuthority> auths = secContext.getAuthentication().getAuthorities();
    if (auths.contains(new SimpleGrantedAuthority("ROLE_CONTRIBUTORS"))) {
        return SAMPLESTACK_CONTRIBUTOR;
    } else {
        return SAMPLESTACK_GUEST;
    }
}

From source file:Main.java

public static <T> void add_news(Collection<T> target, Collection<? extends T> to_add) {
    for (Iterator<? extends T> it = to_add.iterator(); it.hasNext();) {
        T element = it.next();/* w  w w .ja v a 2s.c  om*/
        if (!target.contains(element))
            target.add(element);
    }
}

From source file:Main.java

/**
 * Add elements from the source to the target as long as they don't already
 * exist there. Return the number of items actually added.
 *
 * @param source//from   w  w w.  j av a  2 s.co  m
 * @param target
 * @return int
 */
public static <T> int addWithoutDuplicates(Collection<T> source, Collection<T> target) {

    int added = 0;

    for (T item : source) {
        if (target.contains(item)) {
            continue;
        }
        target.add(item);
        added++;
    }

    return added;
}