Returns true iff at least one element is in both collections. - Java java.util

Java examples for java.util:Collection Element

Description

Returns true iff at least one element is in both collections.

Demo Code

import java.lang.reflect.Array;
import java.util.Collection;
import java.util.Enumeration;
import java.util.Iterator;
import java.util.Map;

public class Main {
  public static void main(String[] argv) {
    Collection coll1 = java.util.Arrays.asList("asdf", "java2s.com");
    Collection coll2 = java.util.Arrays.asList("asdf", "java2s.com");
    System.out.println(containsAny(coll1, coll2));
  }/*from  ww w  .  j  a  va 2  s .c  o  m*/

  /**
   * Returns <code>true</code> iff at least one element is in both collections.
   * <p>
   * In other words, this method returns <code>true</code> iff the
   * {@link #intersection} of <i>coll1</i> and <i>coll2</i> is not empty.
   * 
   * @param coll1
   *          the first collection, must not be null
   * @param coll2
   *          the first collection, must not be null
   * @return <code>true</code> iff the intersection of the collections is
   *         non-empty
   * 
   * @see #intersection
   */
  public static <E> boolean containsAny(final Collection<E> coll1, final Collection<E> coll2) {
    if (coll1 == null || coll2 == null) {
      return false;
    }
    if (coll1.size() < coll2.size()) {
      for (E e : coll1) {
        if (coll2.contains(e)) {
          return true;
        }
      }
    } else {
      for (E e : coll2) {
        if (coll1.contains(e)) {
          return true;
        }
      }

    }
    return false;
  }

  /**
   * Gets the size of the collection/iterator specified.
   * <p>
   * This method can handles objects as follows
   * <ul>
   * <li>Collection - the collection size
   * <li>Map - the map size
   * <li>Array - the array size
   * <li>Iterator - the number of elements remaining in the iterator
   * <li>Enumeration - the number of elements remaining in the enumeration
   * </ul>
   * 
   * @param object
   *          the object to get the size of
   * @return the size of the specified collection
   * @throws IllegalArgumentException
   *           thrown if object is not recognised or null
   * @since Commons Collections 3.1
   */
  public static int size(Object object) {
    int total = 0;
    if (object instanceof Map) {
      total = ((Map<?, ?>) object).size();
    } else if (object instanceof Collection) {
      total = ((Collection<?>) object).size();
    } else if (object instanceof Object[]) {
      total = ((Object[]) object).length;
    } else if (object instanceof Iterator) {
      Iterator<?> it = (Iterator<?>) object;
      while (it.hasNext()) {
        total++;
        it.next();
      }
    } else if (object instanceof Enumeration) {
      Enumeration<?> it = (Enumeration<?>) object;
      while (it.hasMoreElements()) {
        total++;
        it.nextElement();
      }
    } else if (object == null) {
      throw new IllegalArgumentException("Unsupported object type: null");
    } else {
      try {
        total = Array.getLength(object);
      } catch (IllegalArgumentException ex) {
        throw new IllegalArgumentException("Unsupported object type: " + object.getClass().getName());
      }
    }
    return total;
  }

}

Related Tutorials