Example usage for java.util Collection toArray

List of usage examples for java.util Collection toArray

Introduction

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

Prototype

Object[] toArray();

Source Link

Document

Returns an array containing all of the elements in this collection.

Usage

From source file:Main.java

@SuppressWarnings("unchecked")
public static <E> E fromLast(Collection<E> collection, int fromLast) {
    return (E) collection.toArray()[collection.size() - 1 - fromLast];
}

From source file:Main.java

final static public Object first(Collection<Object> collection) {
    Object[] obj = collection.toArray();
    if (obj.length > 0) {
        return obj[0];
    } else {// ww w. j av  a  2 s  .c  o m
        return null;
    }
}

From source file:Main.java

public static void appendCollection(Node node, Collection c, String tag) {
    appendCollection(node, c.toArray(), tag);
}

From source file:Main.java

final static public Object first(Collection collection) {
    Object[] obj = collection.toArray();
    if (obj.length > 0) {
        return obj[0];
    } else {//  w w w  .  j a va  2 s  .c  o m
        return null;
    }
}

From source file:Main.java

public static String collectionToString(Collection<? extends Object> array) {
    String string = "'" + Arrays.toString(array.toArray()).replaceAll("\\[|\\]", "").replaceAll(", ", "','")
            + "'";
    return string;
}

From source file:Main.java

/**
 * Convert collection to array/*from w ww .j  a v a2s . c  om*/
 *
 * @param c
 * @return
 */
public static Object[] listToArray(Collection<?> c) {
    if (!isEmpty(c)) {
        return c.toArray();
    }
    return null;
}

From source file:Main.java

public static String join(Collection<?> collection, String join) {
    Object[] obj = collection.toArray();
    String[] arr = new String[obj.length];
    for (int i = 0; i < obj.length; i++) {
        arr[i] = String.valueOf(obj[i]);
    }//from   ww w  . j  a va  2  s.  c om
    return join(arr, join);
}

From source file:Main.java

public static String mergeAllElements(Collection<String> collection) {
    StringBuffer stringBuffer = new StringBuffer();
    Object[] str = collection.toArray();

    for (int i = 0; i < str.length; i++) {
        if (i == str.length - 1) {
            stringBuffer.append(str[i].toString());
        } else {// w  w w  .j  a v  a 2 s .  c  om
            stringBuffer.append(str[i].toString()).append(",");
        }
    }

    return stringBuffer.toString();
}

From source file:Main.java

public static <T> Enumeration<T> toEnumeration(final Collection<T> collection) {
    return new Enumeration<T>() {

        Object[] array = collection.toArray();
        int index = 0;

        public boolean hasMoreElements() {
            return (index < array.length);
        }/*from w ww.j  a  v  a  2  s  .  c  o m*/

        @SuppressWarnings("unchecked")
        public T nextElement() {
            return (T) array[index++];
        }
    };
}

From source file:Main.java

public static Collection rightDifference(final Collection c1, final Collection c2) {
    final List result = new ArrayList();
    final Object[] members_c1 = c1.toArray();
    for (int i = 0; i < members_c1.length; i++) {
        if (!c2.contains(members_c1[i])) {
            result.add(members_c1[i]);/*w ww.j  a v  a2 s  .c  o  m*/
        }
    }
    return result;
}