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

/**
 * Implementation of the OCL//w  w  w.j ava2 s  .c om
 * <ul>
 * <li><tt>OrderedSet::at(index : Integer) : T</tt></li>
 * <li><tt>Sequence::at(index : Integer) : T</tt></li>
 * </ul>
 * operations.
 * 
 * @param self the source collection
 * @param index the 1-based (in OCL fashion) index
 * @return the object at the specified index of the source collection
 * 
 * @throws IndexOutOfBoundsException if the index is out of bounds
 */
public static <E> E at(Collection<E> self, int index) {
    index = index - 1;

    if (index < 0 || index >= self.size()) {
        throw new IndexOutOfBoundsException("index: " + (index + 1) + ", size: " //$NON-NLS-1$ //$NON-NLS-2$
                + self.size());
    }

    int curr = 0;
    for (Iterator<E> it = self.iterator(); it.hasNext();) {
        E object = it.next();
        if (curr++ == index) {
            return object;
        }
    }
    return null; // undefined
}

From source file:Main.java

/**
 * Implementation of the OCL//from  w  w  w . j a  v a 2 s .  c o  m
 * <ul>
 * <li><tt>OrderedSet::at(index : Integer) : T</tt></li>
 * <li><tt>Sequence::at(index : Integer) : T</tt></li>
 * </ul>
 * operations.
 * 
 * @param self the source collection
 * @param index the 1-based (in OCL fashion) index
 * @return the object at the specified index of the source collection
 */
public static <E> E at(Collection<E> self, int index) {
    index = index - 1;

    if (index < 0 || index >= self.size()) {
        return null; // undefined
    }

    int curr = 0;
    for (Iterator<E> it = self.iterator(); it.hasNext();) {
        E object = it.next();
        if (curr++ == index) {
            return object;
        }
    }
    return null; // undefined
}

From source file:Main.java

/**
 * Transforms a collection of strings into a comma delimited string, where
 * each component get single-quoted.//from   ww w.j  a va  2s.  com
 *
 * @param elements the collection of Integers
 * @return a comma delimited String.
 */
public static String getQuotedCommaDelimitedString(Collection<String> elements) {
    if (elements != null && elements.size() > 0) {
        final StringBuffer buffer = new StringBuffer();

        for (String element : elements) {
            buffer.append("'").append(element.toString()).append("', ");
        }

        return buffer.substring(0, buffer.length() - ", ".length());
    }

    return null;
}

From source file:com.qwazr.utils.ArrayUtils.java

public static int[] toPrimitiveInt(Collection<Integer> collection) {
    int[] array = new int[collection.size()];
    int i = 0;//from  ww  w  .j ava  2 s.co  m
    for (int val : collection)
        array[i++] = val;
    return array;
}

From source file:Main.java

/**
 * Returns <tt>true</tt> iff the given {@link Collection}s contain exactly
 * the same elements with exactly the same cardinality.
 * <p>// ww  w . j av a 2 s  . c o m
 * That is, iff the cardinality of <i>e</i> in <i>a</i> is equal to the
 * cardinality of <i>e</i> in <i>b</i>, for each element <i>e</i> in
 * <i>a</i> or <i>b</i>.
 */
public static boolean isEqualCollection(final Collection a, final Collection b) {
    if (a.size() != b.size()) {
        return false;
    } else {
        Map mapa = getCardinalityMap(a);
        Map mapb = getCardinalityMap(b);
        if (mapa.size() != mapb.size()) {
            return false;
        } else {
            Iterator it = mapa.keySet().iterator();
            while (it.hasNext()) {
                Object obj = it.next();
                if (getFreq(obj, mapa) != getFreq(obj, mapb)) {
                    return false;
                }
            }
            return true;
        }
    }
}

From source file:Main.java

public static String recursiveDeepToString(final Collection<?> collection) {
    final StringBuilder sb = new StringBuilder(3 * collection.size());
    sb.append("[");
    final Iterator<?> iter = collection.iterator();
    if (iter.hasNext()) {
        final Object item = iter.next();
        if (item instanceof Collection) {
            final Collection<?> subCollection = (Collection<?>) item;
            sb.append(recursiveDeepToString(subCollection));
        } else// w ww .  j  a v  a2 s  . c  om
            sb.append(item);
    }
    while (iter.hasNext()) {
        final Object item = iter.next();
        sb.append(", ");
        if (item instanceof Collection) {
            final Collection<?> subCollection = (Collection<?>) item;
            sb.append(recursiveDeepToString(subCollection));
        } else
            sb.append(item);
    }
    sb.append("]");
    return sb.toString();
}

From source file:Main.java

public static <T extends Comparable<? super T>> int compareAsList(Collection<? extends T> list1,
        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;
    }/*  w w  w .  j a  v  a 2 s .  c  om*/
    return 0;
}

From source file:Main.java

public static <T> int compareAsList(Comparator<? super T> elementComparator, Collection<? extends T> list1,
        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 = elementComparator.compare(element1, elements2.next());
        if (res != 0)
            return res;
    }/*from   w  w w  . j  a va 2s  .com*/
    return 0;
}

From source file:com.qwazr.utils.ArrayUtils.java

public static float[] toPrimitiveFloat(Collection<Float> collection) {
    float[] array = new float[collection.size()];
    int i = 0;/*from   w  w  w  . jav a  2  s  . c om*/
    for (float val : collection)
        array[i++] = val;
    return array;
}

From source file:Main.java

/**
 * //ww w  . j a va2s.  c om
 * 
 */
public static <T> T[] toArray(Collection<T> collection, Class<T> elementClass) {
    return toArray(collection, elementClass, 0, collection.size());
}