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:com.shenit.commons.utils.CollectionUtils.java

/**
 * Found out the item that col1 contains but col2 not contains.
 * @param col1 Collection 1/*from w  w w  .ja v a  2s  .  c  o  m*/
 * @param col2 Collection 2
 * @return The items only in col1 but not col2
 */
@SuppressWarnings("unchecked")
public static <T> Collection<T> diff(Collection<T> col1, Collection<T> col2) {
    if (ValidationUtils.isEmpty(col1))
        return col2;
    else if (ValidationUtils.isEmpty(col2))
        return col1;
    try {
        Collection<T> diff = col1.getClass().newInstance();
        col1.stream().forEach((item1) -> {
            if (col2.parallelStream().noneMatch((item2) -> ValidationUtils.eq(item1, item2))) {
                diff.add(item1);
            }
        });
        return diff;
    } catch (InstantiationException | IllegalAccessException e) {
        LOG.warn("[diff] Could not create instance for {} with empty parameter constructor", col1.getClass());
    }
    return null;
}

From source file:CollectionsX.java

/** Adds all elements returned by the enumerator to a collection.
 * @param enm the enumeration; null is OK
 * @return the number element being added
 *///from   w  ww  .  java  2 s.c  o m
public static final int addAll(Collection col, Enumeration enm) {
    int cnt = 0;
    if (enm != null)
        for (; enm.hasMoreElements(); ++cnt)
            col.add(enm.nextElement());
    return cnt;
}

From source file:ObjectInspector.java

/**
 * Recurses up the inheritance chain and collects all the fields
 * /*w  ww  . j  a  va  2  s.  c  om*/
 * @param fields
 *           The collection of fields found so far
 * @param c
 *           The class to get fields from
 */
private static void getFields(Collection<Field> fields, Class c) {
    for (Field f : c.getDeclaredFields()) {
        fields.add(f);
    }

    if (c.getSuperclass() != null) {
        getFields(fields, c.getSuperclass());
    }
}

From source file:com.oltpbenchmark.util.CollectionUtil.java

/**
 * Add all the items in the array to a Collection
 * @param <T>/*from   w ww  .  j  a  va  2  s .  c o  m*/
 * @param data
 * @param items
 */
public static <T> Collection<T> addAll(Collection<T> data, T... items) {
    for (T i : (T[]) items) {
        data.add(i);
    }
    return (data);
}

From source file:gov.va.chir.tagline.dao.FileDao.java

public static Collection<Document> loadScoringLines(final File file, boolean hasHeader) throws IOException {
    final Map<String, Map<Integer, Line>> docMap = new HashMap<String, Map<Integer, Line>>();

    // Assumes tab-delimited columns. May not be sorted.
    // Column labels may be on first line
    // Col 0 = NoteID
    // Col 1 = LineID
    // Col 2 = Text
    final int POS_DOC_ID = 0;
    final int POS_LINE_ID = 1;
    final int POS_TEXT = 2;

    final List<String> contents = FileUtils.readLines(file);

    for (int i = (hasHeader ? 1 : 0); i < contents.size(); i++) {
        final String l = contents.get(i);
        final String[] array = l.split("\t");

        if (!docMap.containsKey(array[POS_DOC_ID])) {
            docMap.put(array[POS_DOC_ID], new TreeMap<Integer, Line>());
        }//from  ww  w  .j  av a2  s  .  c o  m

        int lineId = NumberUtils.toInt(array[POS_LINE_ID]);
        final Line line = new Line(lineId, array[POS_TEXT]);

        docMap.get(array[POS_DOC_ID]).put(lineId, line);
    }

    final Collection<Document> docs = new ArrayList<Document>();

    for (String docId : docMap.keySet()) {
        docs.add(new Document(docId, new ArrayList<Line>(docMap.get(docId).values())));
    }

    return docs;
}

From source file:Main.java

public static Collection<String> getXmlFilenames(String path) throws NullPointerException {
    Collection<String> results = new ArrayList<>();
    File folder = new File(path);

    // We will grab all XML files from the target directory.
    File[] listOfFiles = folder.listFiles();
    if (listOfFiles != null) {
        String file;//  www.  j a  va  2 s .  c  o  m
        for (int i = 0; i < listOfFiles.length; i++) {
            if (listOfFiles[i].isFile()) {
                file = listOfFiles[i].getAbsolutePath();
                if (file.endsWith(".xml") || file.endsWith(".xml")) {
                    results.add(file);
                }
            }
        }
    }
    return results;
}

From source file:com.netflix.spinnaker.clouddriver.kubernetes.v2.caching.agent.KubernetesCacheDataConverter.java

static void addSingleRelationship(Map<String, Collection<String>> relationships, String account,
        String namespace, String fullName) {
    Pair<KubernetesKind, String> triple = KubernetesManifest.fromFullResourceName(fullName);
    KubernetesKind kind = triple.getLeft();
    String name = triple.getRight();

    Collection<String> keys = relationships.get(kind.toString());

    if (keys == null) {
        keys = new ArrayList<>();
    }/*from   w ww.j a v a 2s  .c o  m*/

    keys.add(Keys.infrastructure(kind, account, namespace, name));

    relationships.put(kind.toString(), keys);
}

From source file:gov.va.chir.tagline.dao.FileDao.java

public static Collection<Document> loadLabeledLines(final File file, boolean hasHeader) throws IOException {
    final Map<String, Map<Integer, Line>> docMap = new HashMap<String, Map<Integer, Line>>();

    // Assumes tab-delimited columns. May not be sorted.
    // Column labels may be on first line
    // Col 0 = NoteID
    // Col 1 = LineID
    // Col 2 = Class
    // Col 3 = Text
    final int POS_DOC_ID = 0;
    final int POS_LINE_ID = 1;
    final int POS_CLASS = 2;
    final int POS_TEXT = 3;

    final List<String> contents = FileUtils.readLines(file);

    for (int i = (hasHeader ? 1 : 0); i < contents.size(); i++) {
        final String l = contents.get(i);
        final String[] array = l.split("\t");

        if (!docMap.containsKey(array[POS_DOC_ID])) {
            docMap.put(array[POS_DOC_ID], new TreeMap<Integer, Line>());
        }/*from w w w .j av a2  s.  com*/

        int lineId = NumberUtils.toInt(array[POS_LINE_ID]);

        final Line line = new Line(lineId, (array.length == POS_TEXT ? "" : array[POS_TEXT]), // blank lines have no content 
                array[POS_CLASS]);

        docMap.get(array[POS_DOC_ID]).put(lineId, line);
    }

    final Collection<Document> docs = new ArrayList<Document>();

    for (String docId : docMap.keySet()) {
        docs.add(new Document(docId, new ArrayList<Line>(docMap.get(docId).values())));
    }

    return docs;
}

From source file:com.oltpbenchmark.util.CollectionUtil.java

/**
 * Add all of the items from the Iterator into the given collection
 * @param <T>/*from   w  w w  .  j  a va2  s  .c o  m*/
 * @param data
 * @param items
 */
public static <T> Collection<T> addAll(Collection<T> data, Iterator<T> items) {
    while (items.hasNext()) {
        data.add(items.next());
    } // WHILE
    return (data);
}

From source file:Main.java

private static void getFiles(File aRootDir, Boolean aIsGetRelativePath, File aDirPath, boolean aIsContainSubDir,
        FilenameFilter aFilenameFilter, Collection<String> aOutList) {
    if (aOutList == null) {
        aOutList = new ArrayList<String>();
    }/*from   www .  j a v  a2  s  .  c  o  m*/

    File[] files = aDirPath.listFiles(aFilenameFilter);

    for (int i = 0; i < files.length; i++) {
        File f = files[i];
        if (f.isFile()) {
            String strPath = f.getPath();
            if (aRootDir != null && aIsGetRelativePath) {
                strPath = strPath.substring(aRootDir.getPath().length());
            }
            aOutList.add(strPath);
            if (!aIsContainSubDir)
                break;
        } else if (f.isDirectory() && f.getPath().indexOf("/.") == -1)
            getFiles(aRootDir, aIsGetRelativePath, f, aIsContainSubDir, aFilenameFilter, aOutList);
    }
}