Example usage for java.util Set retainAll

List of usage examples for java.util Set retainAll

Introduction

In this page you can find the example usage for java.util Set retainAll.

Prototype

boolean retainAll(Collection<?> c);

Source Link

Document

Retains only the elements in this set that are contained in the specified collection (optional operation).

Usage

From source file:trustframework.utils.Similarity.java

/**
 * Calculates jaccard similarity between two collections
 * @param <T> Type of collection elements
 * @param c1 collection 1/*w  w w  . j  a v  a 2s . com*/
 * @param c2 collection 2
 * @return jaccard similarity [0,1]
 */
public static <T> double jaccardSimilarity(Collection<T> c1, Collection<T> c2) {
    Set<T> intersection = new HashSet<>(c1);
    intersection.retainAll(c2);
    Set<T> union = new HashSet<>(c1);
    union.addAll(c2);
    return (double) intersection.size() / (double) union.size();
}

From source file:com.github.blindpirate.gogradle.util.CollectionUtils.java

public static <T> Set<T> intersection(final Collection<T> c1, final Collection<T> c2) {
    Set<T> set = new HashSet<>(c1);
    set.retainAll(c2);
    return set;/* www.  j a va2  s .c o  m*/
}

From source file:Main.java

/**
 * Given two collections of elements of type <T>, return a collection with the items which only appear in both lists
 * //  w  w w.j  a va2 s  . co  m
 * @param <T>
 *            Type
 * @param collection1
 *            Collection #1
 * @param collection2
 *            Collection #2
 * @return Collection with items common to both lists
 */
public static <T> Collection<T> intersect(Collection<T> collection1, Collection<T> collection2) {
    if (isEmpty(collection1) || isEmpty(collection2)) {
        return Collections.emptyList();
    }

    Set<T> intersection = new HashSet<T>(collection1);

    intersection.retainAll(collection2);

    return intersection;
}

From source file:com.minlia.cloud.framework.common.util.SearchCommonUtil.java

public static boolean validateParameters(final Set<String> paramKeys) {
    if (paramKeys.retainAll(Lists.newArrayList(// @formatter:off
            SearchField.name.toString(), QueryConstants.NAME_NEG,

            SearchField.loginName.toString(), QueryConstants.LOGIN_NAME_NEG,

            SearchField.id.toString(), QueryConstants.ID_NEG,

            SearchField.email.toString(), QueryConstants.EMAIL_NEG,

            SearchField.tenant.toString(), QueryConstants.TENANT_NEG,

            SearchField.locked.toString(), QueryConstants.LOCKED_NEG,

            SearchField.description.toString(), QueryConstants.DESCRIPTION_NEG))) { // @formatter:on
        return false;
    }//from   w  w  w. ja va 2s .  c  o  m
    return true;
}

From source file:Main.java

/**
 * This method returns a new HashSet which is the intersection of the two Set parameters, based on {@link Object#equals(Object) equality}
 * of their elements./* ww  w.  j a  va 2s. com*/
 * Any references in the resultant set will be to elements within set1.
 * 
 * @return a new HashSet whose values represent the intersection of the two Sets.
 */
public static <T> Set<T> intersect(Set<? extends T> set1, Set<? extends T> set2) {
    if (set1 == null || set1.isEmpty() || set2 == null || set2.isEmpty()) {
        return Collections.emptySet();
    }

    Set<T> result = new HashSet<T>();
    result.addAll(set1);

    result.retainAll(set2);

    return result;
}

From source file:se.uu.it.cs.recsys.ruleminer.api.RuleMiner.java

private static boolean hasIntersection(Set<Integer> a, Set<Integer> b) {
    Set<Integer> aClone = new HashSet<>(a);
    Set<Integer> bClone = new HashSet<>(b);

    aClone.retainAll(bClone);

    return !aClone.isEmpty();
}

From source file:com.mirth.connect.util.ChannelDependencyUtil.java

/**
 * Given a set of channel IDs and a dependency graph, this method splits the IDs into a set of
 * unordered IDs (that have no dependents or dependencies), and a list of multiple sets of
 * ordered IDs. The list is ordered such that the IDs in any given set are not interdependent on
 * each other, but all the IDs in the set are individually dependent on one or more IDs in one
 * of the subsequent sets./*from  ww  w. ja v a2s  .  c om*/
 */
public static OrderedChannels getOrderedChannels(Set<String> channelIds, ChannelDependencyGraph graph) {
    Set<String> unorderedIds = new HashSet<String>(channelIds);
    List<Set<String>> orderedIds = new ArrayList<Set<String>>();

    for (Set<String> set : graph.getOrderedElements()) {
        // Only include IDs in the set passed in
        set.retainAll(channelIds);

        if (!set.isEmpty()) {
            orderedIds.add(set);
            unorderedIds.removeAll(set);
        }
    }

    return new OrderedChannels(unorderedIds, orderedIds);
}

From source file:org.elasticsearch.xpack.qa.sql.jdbc.JdbcIntegrationTestCase.java

public static String randomKnownTimeZone() {
    // We use system default timezone for the connection that is selected randomly by TestRuleSetupAndRestoreClassEnv
    // from all available JDK timezones. While Joda and JDK are generally in sync, some timezones might not be known
    // to the current version of Joda and in this case the test might fail. To avoid that, we specify a timezone
    // known for both Joda and JDK
    Set<String> timeZones = new HashSet<>(DateTimeZone.getAvailableIDs());
    timeZones.retainAll(Arrays.asList(TimeZone.getAvailableIDs()));
    List<String> ids = new ArrayList<>(timeZones);
    Collections.sort(ids);/*from www.  j av  a2 s.  co m*/
    return randomFrom(ids);
}

From source file:com.taobao.metamorphosis.client.producer.SimpleXAMessageProducer.java

static <T> Set<T> intersect(final List<Set<T>> sets) {
    if (sets == null || sets.size() == 0) {
        return null;
    }/*from   w  w w.ja v a2s  .  c  o m*/
    Set<T> rt = sets.get(0);
    for (int i = 1; i < sets.size(); i++) {
        final Set<T> copy = new HashSet<T>(rt);
        copy.retainAll(sets.get(i));
        rt = copy;
    }
    return rt;
}

From source file:com.vmware.admiral.request.compute.NetworkProfileQueryUtils.java

private static void getNetworkConstraints(ServiceHost host, URI referer, ComputeDescription computeDescription,
        HashMap<String, ComputeNetwork> contextComputeNetworks, BiConsumer<Set<String>, Throwable> consumer) {
    DeferredResult<List<ComputeNetwork>> result = DeferredResult
            .allOf(computeDescription.networkInterfaceDescLinks.stream().map(nicDescLink -> {
                Operation op = Operation.createGet(host, nicDescLink).setReferer(referer);

                return host.sendWithDeferredResult(op, NetworkInterfaceDescription.class);
            }).map(nid -> nid.thenCompose(nic -> {
                ComputeNetwork computeNetwork = contextComputeNetworks.get(nic.name);
                if (computeNetwork == null) {
                    throw new LocalizableValidationException(
                            String.format("Could not find context network component with name '%s'.", nic.name),
                            "compute.network.component.not.found", nic.name);

                }//from w  w  w.ja  va 2 s. c o m
                return DeferredResult.completed(computeNetwork);
            })).collect(Collectors.toList()));

    result.whenComplete((all, e) -> {
        if (e != null) {
            consumer.accept(null, e);
            return;
        }

        // Remove networks that don't have any constraints
        all.removeIf(cn -> cn.networkProfileLinks == null || cn.networkProfileLinks.isEmpty());
        if (all.isEmpty()) {
            consumer.accept(null, null);
            return;
        }
        Set<String> networkProfileLinks = all.get(0).networkProfileLinks;
        all.forEach(cn -> networkProfileLinks.retainAll(cn.networkProfileLinks));
        consumer.accept(networkProfileLinks, null);
    });
}