Example usage for java.util Set size

List of usage examples for java.util Set size

Introduction

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

Prototype

int size();

Source Link

Document

Returns the number of elements in this set (its cardinality).

Usage

From source file:Main.java

/**
 * Copies the given {@link Set} into a new {@link Set}.<br>
 * /*  w  ww  . j a va2  s  . c o  m*/
 * 
 * @param <T>
 *            the type of the entries of the set
 * @param data
 *            the given set
 * @return If the given set was empty, a {@link Collections#emptySet()} is
 *         returned<br>
 *         If the given set contained only one element, a
 *         {@link Collections#singleton(Object)}, containing said element,
 *         is returned <br>
 *         If the given set contained more than one element, a
 *         {@link Collections#unmodifiableSet(Set)}, containing the
 *         elements of the given set, is returned.
 */
public static <T> Set<T> copy(Set<T> data) {
    final int size = data.size();

    switch (size) {
    case 0:
        return Collections.emptySet();
    case 1:
        return Collections.singleton(data.iterator().next());
    default:
        return Collections.unmodifiableSet(new HashSet<T>(data));
    }
}

From source file:Main.java

public static long remove(Set set) {
    long start, stop, result = 0;
    for (int i = 0; i < 100; i++) {
        start = System.nanoTime();
        set.remove(set.size() - 1 - i);
        stop = System.nanoTime();
        result += stop - start;/*  w w w  .  jav a 2 s. c  om*/
    }
    return result / 100;
}

From source file:Main.java

/**
 * Produce a new set//from   w  w  w  .  j av a2s  . co  m
 * @param <T>
 * @param set1
 * @param set2
 * @return 
 */
public static <T> Set<T> intersect(Set<T> set1, Set<T> set2) {
    Set<T> a;
    Set<T> b;

    if (set1.size() <= set2.size()) {
        a = set1;
        b = set2;
    } else {
        a = set2;
        b = set1;
    }
    Set<T> intersection = new HashSet<>(a.size() * 4 / 3);
    for (T e : a) {
        if (b.contains(e)) {
            intersection.add(e);
        }
    }
    return intersection;
}

From source file:Utils.java

/**
 * Create a typesafe copy of a raw set.//from   w w w  . j  a  v a  2 s.  c o  m
 * @param rawSet an unchecked set
 * @param type the desired supertype of the entries
 * @param strict true to throw a <code>ClassCastException</code> if the raw set has an invalid entry,
 *               false to skip over such entries (warnings may be logged)
 * @return a typed set guaranteed to contain only entries assignable
 *         to the named type (or they may be null)
 * @throws ClassCastException if some entry in the raw set was not well-typed, and only if <code>strict</code> was true
 */
public static <E> Set<E> checkedSetByCopy(Set rawSet, Class<E> type, boolean strict) throws ClassCastException {
    Set<E> s = new HashSet<E>(rawSet.size() * 4 / 3 + 1);
    Iterator it = rawSet.iterator();
    while (it.hasNext()) {
        Object e = it.next();
        try {
            s.add(type.cast(e));
        } catch (ClassCastException x) {
            if (strict) {
                throw x;
            } else {
                System.out.println("not assignable ");
            }
        }
    }
    return s;
}

From source file:org.logger.event.web.utils.ServerValidationUtils.java

public static void rejectIfNullOrEmpty(Errors errors, Set<?> data, String field, String errorMsg) {
    if (data == null || data.size() == 0) {
        errors.rejectValue(field, errorMsg);
    }/*from   w  w w.j  a  v a 2s  .c  om*/
}

From source file:com.titilink.camel.rest.util.ValidationUtil.java

/**
 * ??JSR-303bean//from   ww  w  .j  a  va2 s.c o  m
 *
 * @param instance 
 * @return
 */
public static <T> Set<RootResponse> validateExtend(T instance, Class<?>... groups) {
    Set<ConstraintViolation<T>> set = validateBean(instance, groups);

    if (set.size() == 0) {
        return null;
    }

    Set<RootResponse> errorBoxes = new HashSet<>(set.size());
    for (ConstraintViolation<T> c : set) {
        String message = c.getMessage();
        if (StringUtils.isNotEmpty(message)) {
            String[] item = message.split(MESSAGE_TEMPLATE_SEPARATOR);
            errorBoxes.add(new RootResponse(item[0], item[1]));
        }
    }
    return errorBoxes;
}

From source file:com.concursive.connect.scheduler.ScheduledJobs.java

/**
 * Scans the jobs path for the given ServletContext
 *
 * @param scheduler/*from   w  w  w .  j  a v a  2  s.c om*/
 * @param context
 * @throws SchedulerException
 */
public static void addJobs(Scheduler scheduler, ServletContext context) throws SchedulerException {
    // Determine the ApplicationPrefs
    ApplicationPrefs prefs = (ApplicationPrefs) scheduler.getContext().get("ApplicationPrefs");
    // Find job files in the jobs path
    Set<String> jobFiles = context.getResourcePaths("/WEB-INF/jobs/");
    if (jobFiles != null && jobFiles.size() > 0) {
        for (String thisFile : jobFiles) {
            if (thisFile.endsWith(".xml")) {
                try {
                    LOG.debug("Adding jobs from... " + thisFile);
                    QuartzUtils.addJobs(scheduler, context.getResource(thisFile), prefs.getPrefs());
                } catch (Exception e) {
                    LOG.error("addJobs exception", e);
                }
            }
        }
    }
}

From source file:com.rapleaf.hank.coordinator.Domains.java

public static DomainVersion getLatestVersionNotOpenNotDefunct(Domain domain) throws IOException {
    Set<DomainVersion> originalVersions = domain.getVersions();
    if (originalVersions == null || originalVersions.size() == 0) {
        return null;
    }/*from   w  w w  .j  av  a 2s. co m*/
    SortedSet<DomainVersion> versions = new TreeSet<DomainVersion>(new ReverseComparator<DomainVersion>());
    versions.addAll(originalVersions);
    for (DomainVersion version : versions) {
        if (DomainVersions.isClosed(version) && !version.isDefunct()) {
            return version;
        }
    }
    return null;
}

From source file:com.linkedin.paldb.impl.GenerateTestData.java

public static Integer[] generateRandomIntKeys(int count, int range, long seed) {
    Random random = new Random(seed);
    Set<Integer> set = new HashSet<Integer>(count);
    while (set.size() < count) {
        set.add(random.nextInt(range));//from www.  java 2 s .  co m
    }
    return set.toArray(new Integer[0]);
}

From source file:de.unisb.cs.st.javalanche.mutation.util.AddMutations.java

private static Set<Integer> getRandomValues(int originalValue, int i) {
    Set<Integer> result = new HashSet<Integer>();
    while (result.size() < i) {
        int nextInt = r.nextInt();
        if (nextInt != originalValue && nextInt != 0 && nextInt != originalValue + 1
                && nextInt != originalValue - 1) {
            result.add(nextInt);/*from  w  w  w. j a  v  a 2  s  .c om*/
        }
    }
    return result;
}