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

/**
 * Copy the given Collection into a String array.
 * The Collection must contain String elements only.
 * @param collection the Collection to copy
 * @return the String array ({@code null} if the passed-in
 * Collection was {@code null})//from  w  ww.j av a  2 s  . c  o m
 */
public static String[] toStringArray(Collection<String> collection) {
    if (collection == null) {
        return null;
    }
    return collection.toArray(new String[collection.size()]);
}

From source file:Main.java

/**
 * /*from ww  w  . ja v  a  2s . c o  m*/
 * @param collection
 * @return String
 */
public static String toToolTip(final Collection<?> collection) {
    if (collection == null || collection.size() == 0) {
        return null;
    }

    final String START_SEPARATOR = "<p>"; //$NON-NLS-1$
    final String END_SEPARATOR = "</p>"; //$NON-NLS-1$
    final StringBuffer buffer = new StringBuffer("<html>"); //$NON-NLS-1$

    for (Iterator<?> iterator = collection.iterator(); iterator.hasNext();) {
        buffer.append(START_SEPARATOR);
        buffer.append(iterator.next());
        buffer.append(END_SEPARATOR);
    }

    buffer.append("</html>"); //$NON-NLS-1$

    return buffer.toString();
}

From source file:com.partydj.search.LuceneSearchProvider.java

private static String getFirst(Map<String, Collection<String>> map, String key) {
    Collection<String> c = map.get(key);
    if (c != null && c.size() > 0) {
        return c.iterator().next();
    }//from  ww w  .  j  a va  2s .c  o  m
    return null;
}

From source file:Main.java

public static boolean equals(Collection<?> collection1, Collection<?> collection2) {
    if (collection1 == collection2) {
        return true;
    }//from w  w w.j av a2s .c  o m

    if (collection1.size() != collection2.size()) {
        return false;
    }

    final Iterator<?> iterator1 = collection1.iterator();
    final Iterator<?> iterator2 = collection2.iterator();

    while (iterator1.hasNext()) {
        final Object object1 = iterator1.next();
        final Object object2 = iterator2.next();

        if ((object1 == null && object2 != null) || (object1 != null && object2 == null)) {
            return false;
        }

        if (object1 != null && !object1.equals(object2)) {
            return false;
        }
    }

    return true;
}

From source file:com.wavemaker.commons.util.WMFileUtils.java

public static Collection<String> findMatchedRelativePaths(String pattern, String basePath) {
    FilePatternMatchVisitor filePatternMatchVisitor = new FilePatternMatchVisitor(pattern, basePath);
    try {/*  ww  w. j a va  2  s  .  c  om*/
        Files.walkFileTree(Paths.get(basePath), filePatternMatchVisitor);
        Collection<Path> matchedFiles = filePatternMatchVisitor.getMatchedPaths();
        List<String> matchedFilePaths = new ArrayList<>(matchedFiles.size());
        for (Path path : matchedFiles) {
            matchedFilePaths.add(path.toString());
        }
        return matchedFilePaths;
    } catch (IOException e) {
        throw new WMRuntimeException("Failed to find matched ignore patterns for " + pattern, e);
    }
}

From source file:es.molabs.io.utils.FileHelper.java

public static URL[] getFiles(URL path, boolean recursive, String... extensions) throws IOException {
    URL[] urls = null;/*from  w  w  w .j av a 2 s  .c o  m*/

    try {
        Collection<File> fileCollection = FileUtils.listFiles(new File(path.toURI()), extensions, recursive);
        urls = new URL[fileCollection.size()];

        Iterator<File> iterator = fileCollection.iterator();
        int index = 0;
        while (iterator.hasNext()) {
            File file = iterator.next();

            urls[index++] = file.toURI().toURL();
        }
    } catch (URISyntaxException USe) {
        throw new IOException(USe);
    }

    return urls;
}

From source file:Main.java

public static File[] listFiles(File directory, boolean recurse, FileFilter filter) {
    if (!recurse) {
        return directory.listFiles(filter);
    } else {//  w ww  . j a v  a 2 s. c  o  m
        Collection<File> mFiles = new java.util.LinkedList<>();
        innerListFiles(mFiles, directory, filter);
        return mFiles.toArray(new File[mFiles.size()]);
    }
}

From source file:Main.java

public static <T, R> R[] mapToArray(Collection<T> inputs, Function<? super T, ? extends R> mapper,
        IntFunction<R[]> arrayGenerator) {
    R[] result = arrayGenerator.apply(inputs.size());
    int idx = 0;/*from  w w  w.  ja v  a2s .c o m*/
    for (T t : inputs) {
        result[idx++] = mapper.apply(t);
    }
    return result;
}

From source file:net.sf.zekr.common.util.CollectionUtils.java

/**
 * @param col collection parameter to be returned as array
 * @return an array of <code>collection.eachElement.toString()</code>.
 *//*from   w w w  .j  a  v a 2s .  co m*/
public static String[] toStringArray(Collection<?> col) {
    String[] s = new String[col.size()];
    int i = 0;
    for (Iterator<?> iter = col.iterator(); iter.hasNext(); i++) {
        Object element = iter.next();
        s[i] = element.toString();
    }
    return s;
}

From source file:Main.java

/**
 * @param a//ww  w  . ja va2 s  .c o  m
 * @param b
 * @return
 */
public static boolean containsSome(Collection a, Collection b) {
    // fast paths
    if (a.size() == 0 || b.size() == 0)
        return false;
    if (a == b)
        return true; // must test after size test.

    if (a instanceof SortedSet && b instanceof SortedSet) {
        SortedSet aa = (SortedSet) a;
        SortedSet bb = (SortedSet) b;
        Comparator bbc = bb.comparator();
        Comparator aac = aa.comparator();
        if (bbc == null && aac == null) {
            Iterator ai = aa.iterator();
            Iterator bi = bb.iterator();
            Comparable ao = (Comparable) ai.next(); // these are ok, since the sizes are != 0
            Comparable bo = (Comparable) bi.next();
            while (true) {
                int rel = ao.compareTo(bo);
                if (rel < 0) {
                    if (!ai.hasNext())
                        return false;
                    ao = (Comparable) ai.next();
                } else if (rel > 0) {
                    if (!bi.hasNext())
                        return false;
                    bo = (Comparable) bi.next();
                } else {
                    return true;
                }
            }
        } else if (bbc.equals(a)) {
            Iterator ai = aa.iterator();
            Iterator bi = bb.iterator();
            Object ao = ai.next(); // these are ok, since the sizes are != 0
            Object bo = bi.next();
            while (true) {
                int rel = aac.compare(ao, bo);
                if (rel < 0) {
                    if (!ai.hasNext())
                        return false;
                    ao = ai.next();
                } else if (rel > 0) {
                    if (!bi.hasNext())
                        return false;
                    bo = bi.next();
                } else {
                    return true;
                }
            }
        }
    }
    for (Iterator it = a.iterator(); it.hasNext();) {
        if (b.contains(it.next()))
            return true;
    }
    return false;
}