Java Collection Contain containsAny(Collection container, Collection contained)

Here you can find the source of containsAny(Collection container, Collection contained)

Description

contains Any

License

Open Source License

Parameter

Parameter Description
container the containing collection
contained the collection with a contained element

Return

true if container contains any of the elements in contained. this is the same as asking if the collections intersect. the terminology is just because it's sometimes easier to think of it this way.

Declaration

public static boolean containsAny(Collection container, Collection contained) 

Method Source Code


//package com.java2s;
// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

import java.util.*;

public class Main {
    /**//from w  w w  .j ava2  s  .co m
     *
     * @param container the containing collection
     * @param contained the collection with a contained element
     * @return true if container contains any of the elements in contained.
     * this is the same as asking if the collections intersect.
     * the terminology is just because it's sometimes easier to think of it this way.
     */
    public static boolean containsAny(Collection container, Collection contained) {

        Collection copy = intersect(container, contained);

        return !copy.isEmpty();
    }

    /**
     *
     * @param container a collection
     * @param contained another collection
     * @return an intersection
     */
    public static Collection intersect(Collection container, Collection contained) {

        Collection copy = new ArrayList(container);
        copy.retainAll(contained);

        return copy;
    }
}

Related

  1. containsAll(final Collection coll1, final Collection coll2)
  2. containSameElements(Collection> sets)
  3. containSameItems(Collection c1, Collection c2)
  4. containsAny(Collection c1, Collection c2)
  5. containsAny(Collection collection1, Collection collection2)
  6. containsAny(Collection col1, Collection col2)
  7. containsAny(Collection source, Collection candidates)
  8. containsAny(Collection source, Object... candidates)
  9. containsAny(Collection collection1, Collection collection2)