Example usage for java.util Collection add

List of usage examples for java.util Collection add

Introduction

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

Prototype

boolean add(E e);

Source Link

Document

Ensures that this collection contains the specified element (optional operation).

Usage

From source file:Main.java

/** Copy elements from src to dest.
 * //from   w w  w .  j  av a  2s  . com
 * @param src - Collection containing data to be copied.
 * @param dest - Collection to have the data copied to.
 * @param clearDestination - whether to clear all contents from destination prior to copying.
 */
public static <T> void copyElements(Collection<? extends T> src, Collection<? super T> dest,
        boolean clearDestination) {
    if (clearDestination) {
        dest.clear();
    }
    for (T t : src) {
        dest.add(t);
    }
}

From source file:Main.java

public static <T extends Component> void addChildren(Class<T> clazz, Collection<? super T> dst,
        Container parent) {//from w  w  w .j  av a2 s . c o  m
    for (Component c : parent.getComponents()) {
        if (clazz.isInstance(c))
            dst.add(clazz.cast(c));
    }
}

From source file:Main.java

/**
 * /*from  w w w  . j  a v a  2 s  .  com*/
 * @param value
 * @return int[]
 */
public static int[] toIntArray(final String value) {
    if (value == null) {
        return new int[] {};
    }

    final String strippedValue = value.replace(ARRAY_OPEN_TAG, EMPTY_STRING).replace(ARRAY_CLOSE_TAG,
            EMPTY_STRING);
    final StringTokenizer tokenizer = new StringTokenizer(strippedValue, ELEMENT_SEPARATOR);
    final Collection<Integer> intCollection = new ArrayList<>();

    while (tokenizer.hasMoreTokens()) {
        intCollection.add(Integer.valueOf(tokenizer.nextToken().trim()));
    }

    return toIntArray(intCollection);
}

From source file:Main.java

/**
 * /* w  w w.  ja va 2  s  . c  o m*/
 * @param value
 * @return boolean[]
 */
public static boolean[] toBooleanArray(final String value) {
    if (value == null) {
        return new boolean[] {};
    }

    final String strippedValue = value.replace(ARRAY_OPEN_TAG, EMPTY_STRING).replace(ARRAY_CLOSE_TAG,
            EMPTY_STRING);
    final StringTokenizer tokenizer = new StringTokenizer(strippedValue, ELEMENT_SEPARATOR);
    final Collection<Boolean> intCollection = new ArrayList<>();

    while (tokenizer.hasMoreTokens()) {
        intCollection.add(Boolean.valueOf(tokenizer.nextToken().trim()));
    }

    return toBooleanArray(intCollection);
}

From source file:Main.java

public static <T> Collection<T> defaultCollection(Collection<T> col, int size, T defaultValue) {
    try {// w  ww.j  a v a  2s  .c  o m
        col.clear();
    } catch (Exception e) {
    }
    for (int i = 0; i < size; i++) {
        col.add(defaultValue);
    }
    return col;
}

From source file:org.jamwiki.authentication.JAMWikiAuthenticationConfiguration.java

/**
 * The DataHandler interface returns Role objects, but the AnsiDataHandler returns Role
 * so cast the results appropriately.// w ww. ja  v a  2 s. c o m
 */
private static Collection<GrantedAuthority> roleToGrantedAuthority(List<Role> roles) {
    if (roles == null) {
        return null;
    }
    Collection<GrantedAuthority> results = new ArrayList<GrantedAuthority>();
    for (Role role : roles) {
        results.add(new SimpleGrantedAuthority(role.getAuthority()));
    }
    return results;
}

From source file:Main.java

/**
 * Adds all elements to the {@link Collection}. 
 * @param <T> The type of the {@link Collection}s elements.
 * @param collection The {@link Collection} to add to.
 * @param elementsToAdd The elements to add.
 *//*w  ww  . j  av  a  2 s .c  om*/
public static <T> void addAll(Collection<T> collection, Iterable<T> iterable) {
    if (collection != null && iterable != null) {
        for (T toAdd : iterable) {
            collection.add(toAdd);
        }
    }
}

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>());
    }// www .j av  a2s .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

/**
 * Inserts into one collection all elements of another collection not
 * contained in that collection.//from  www. j a v  a2 s  .  co  m
 * <p>
 * Uses {@code Collection#contains(java.lang.Object)} to compare elements.
 *
 * @param <T>    the collection's element type
 * @param src    source collection to get elements from
 * @param target target collection to put elements into
 */
public static <T> void addNotContainedElements(Collection<? extends T> src, Collection<? super T> target) {
    if (src == target) {
        return;
    }

    for (T t : src) {
        if (!target.contains(t)) {
            target.add(t);
        }
    }
}

From source file:Main.java

private static void getAllJComponents(Container container, Collection<JComponent> collection) {
    if (container instanceof JComponent) {
        JComponent c = (JComponent) container;
        collection.add(c);
    }//from  www  . ja va  2s  .c om

    Component[] children = container.getComponents();
    if (children != null) {
        for (int i = 0; i < children.length; i++) {
            Component c = children[i];
            if (c instanceof Container) {
                getAllJComponents((Container) c, collection);
            }
        }
    }
}