Example usage for java.util Collection size

List of usage examples for java.util Collection size

Introduction

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

Prototype

int size();

Source Link

Document

Returns the number of elements in this collection.

Usage

From source file:Main.java

/**
 * Returns <code>true</code> iff at least one element is in both collections.
 * <p/>//from  www. ja v  a 2s. c om
 * 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 boolean containsAny(final Collection coll1, final Collection coll2) {
    if (coll1.size() < coll2.size()) {
        for (Iterator it = coll1.iterator(); it.hasNext();) {
            if (coll2.contains(it.next()))
                return true;
        }
    } else {
        for (Iterator it = coll2.iterator(); it.hasNext();) {
            if (coll1.contains(it.next()))
                return true;
        }
    }
    return false;
}

From source file:Main.java

public static String getSetClause(String fieldName, Collection col, String in) {
    final StringBuilder builder = new StringBuilder();

    if (col.size() > 0) {
        builder.append(fieldName + " " + in + " (");
    }/*  w w  w .  j  a  v  a 2 s. c o  m*/

    for (int i = 0; i < col.size(); i++) {
        builder.append("?, ");
    }

    if (col.size() > 0) {
        builder.setLength(builder.length() - 2);
        builder.append(")");
    }

    return builder.toString();
}

From source file:Main.java

/**
 * Null safe creation of a {@link ArrayList} out of a given collection. The returned {@link ArrayList} is modifiable.
 * The result list is never null and does not contain any null elements.
 *
 * @param c//from   w  ww  . jav  a2s . c  o m
 * @return an {@link ArrayList} containing the given collection's elements. Never null
 */
public static <T> ArrayList<T> arrayListWithoutNullElements(Collection<? extends T> c) {
    if (c != null) {
        ArrayList<T> list = new ArrayList<T>(c.size());
        for (T o : c) {
            if (o != null) {
                list.add(o);
            }
        }
        return list;
    }
    return emptyArrayList();
}

From source file:Main.java

static <K, V> int getCollectionSize(final Map<K, ? extends Collection<V>> map, final K key) {
    final Collection<V> values = map.get(key);
    if (values == null) {
        return 0;
    } else {// w  ww . j a  va 2 s. c  om
        return values.size();
    }
}

From source file:Main.java

public static String join(final Collection<?> collection, final String separator) {
    if (collection == null || collection.size() == 0) {
        return null;
    }//  ww  w  .  j  a v a  2s  .c  om
    StringBuilder sb = new StringBuilder(collection.size() * 7);
    Iterator<?> iterator = collection.iterator();
    sb.append(iterator.next());
    while (iterator.hasNext()) {
        sb.append(separator);
        sb.append(iterator.next());
    }
    return sb.toString();
}

From source file:Comparing.java

public static <T> boolean haveEqualElements(Collection<T> a, Collection<T> b) {
    if (a.size() != b.size()) {
        return false;
    }//w ww . ja v a2 s  . co m

    Set<T> aSet = new HashSet<T>(a);
    for (T t : b) {
        if (!aSet.contains(t)) {
            return false;
        }
    }
    return true;
}

From source file:de.uni.bremen.monty.moco.CompileFilesBaseTest.java

protected static Collection<Object[]> buildParameterObject(Collection<File> programFiles) {
    Object[][] a = new Object[programFiles.size()][1];
    int i = 0;/*from  www  .jav  a  2 s  .  co  m*/
    for (File programFile : programFiles) {
        a[i++] = new Object[] { programFile, programFile.getName() };
    }
    return Arrays.asList(a);
}

From source file:Main.java

public static <T> Collection<Collection<T>> split(Collection<T> coll, int batchSize) {
    Collection<Collection<T>> batches = new ArrayList<>(batchSize);
    int batchCount = coll.size() / batchSize;
    for (int i = 0; i < batchCount; i++) {
        batches.add(new ArrayList<T>());
    }/* w  w w.j a  v a  2  s  . c  o m*/

    int index = 0;

    for (T t : coll) {
        ((ArrayList<Collection<T>>) batches).get(index).add(t);
        index = (index + 1) % batchCount;
    }

    return batches;
}

From source file:Main.java

/**
 * Compute the common root xpath for all provided xpaths
 * @param xpaths list of xpaths// ww  w  . j a  v  a 2  s .  c o m
 * @return common root of provided list
 */
public static String commonRoot(Collection<String> xpaths) {

    String commonPath = "";
    String[][] folders = new String[xpaths.size()][];

    int c = 0;
    for (String xpath : xpaths) {
        folders[c++] = xpath.split("/"); //split on file separator
    }

    for (int j = 0; j < folders[0].length; j++) {
        String thisFolder = folders[0][j]; //grab the next folder name in the first path
        boolean allMatched = true; //assume all have matched in case there are no more paths
        for (int i = 1; i < folders.length && allMatched; i++) { //look at the other paths
            if (folders[i].length < j) { //if there is no folder here
                allMatched = false; //no match
                break; //stop looking because we've gone as far as we can
            }
            //otherwise
            allMatched &= folders[i][j].equals(thisFolder); //check if it matched
        }
        if (allMatched) { //if they all matched this folder name
            commonPath += thisFolder + "/"; //add it to the answer
        } else {//otherwise
            break;//stop looking
        }
    }
    return commonPath;
}

From source file:Main.java

public static <T extends Comparable<? super T>> int compareAsList(@Nonnull Collection<? extends T> list1,
        @Nonnull Collection<? extends T> list2) {
    int res = Ints.compare(list1.size(), list2.size());
    if (res != 0)
        return res;
    Iterator<? extends T> elements2 = list2.iterator();
    for (T element1 : list1) {
        res = element1.compareTo(elements2.next());
        if (res != 0)
            return res;
    }//  ww  w .j  av a 2  s.c  o  m
    return 0;
}