Example usage for java.util List addAll

List of usage examples for java.util List addAll

Introduction

In this page you can find the example usage for java.util List addAll.

Prototype

boolean addAll(Collection<? extends E> c);

Source Link

Document

Appends all of the elements in the specified collection to the end of this list, in the order that they are returned by the specified collection's iterator (optional operation).

Usage

From source file:Main.java

public static <T> List<T> union(final List<T> list1, final List<T> list2) {
    final List<T> result = new ArrayList<T>();
    result.addAll(list1);
    result.addAll(list2);/*from ww w.jav a 2 s .c  o m*/

    return result;
}

From source file:Main.java

public static String implementProjection(List<String> columns, String[] projection,
        Map<String, String> translation) {
    List<String> localColumns = new ArrayList<String>();
    if (projection == null) {
        localColumns.addAll(columns);
    } else {//from   w  w  w  .j  ava  2  s .c o  m
        for (int i = 0; i < projection.length; i++) {
            if (columns.contains(projection[i])) {
                localColumns.add(projection[i]);
            }
        }
    }
    StringBuffer sb = new StringBuffer();
    for (int i = 0; i < localColumns.size(); i++) {
        if (i > 0) {
            sb.append(", ");
        }
        if (translation.containsKey(localColumns.get(i))) {
            sb.append(translation.get(localColumns.get(i)));
        } else {
            sb.append(localColumns.get(i));
        }
    }
    return sb.toString();
}

From source file:Main.java

/**
 * Returns the power set of the given input list, only considering elements
 * at or after index <code>start</code>.
 *///www  .ja v a  2 s  .  c o m
private static <T> List<List<T>> getPowerSet(List<T> input, int start) {
    ArrayList<List<T>> result = new ArrayList<>();
    if (start >= input.size()) {
        result.add(new ArrayList<T>());
    } else {
        T element = input.get(start);
        for (List<T> list : getPowerSet(input, start + 1)) {
            List<T> copy = new ArrayList<>();
            copy.add(element);
            copy.addAll(list);

            result.add(list);
            result.add(copy);
        }
    }
    return result;
}

From source file:Main.java

public static <T1, T2> List<T2> concat(List<T1> lst, Function<T1, List<T2>> mapper) {
    List<T2> lst_ = new ArrayList<>(lst.size());
    for (T1 elem : lst)
        lst_.addAll(mapper.apply(elem));
    return lst_;/*from   w  w w  .j  ava2  s  . co  m*/
}

From source file:edu.wpi.checksims.util.PairGenerator.java

/**
 * Generate all possible unique, unordered pairs of submissions.
 *
 * @param submissions Submissions to generate pairs from
 * @return Set of all unique, unordered pairs of submissions
 *//*www  .jav  a 2  s . c om*/
public static Set<Pair<Submission, Submission>> generatePairs(Set<Submission> submissions) {
    checkNotNull(submissions);
    checkArgument(submissions.size() >= 2, "Cannot generate pairs with less than 2 submissions!");

    Set<Pair<Submission, Submission>> pairs = new HashSet<>();

    List<Submission> remaining = new ArrayList<>();
    remaining.addAll(submissions);

    while (remaining.size() >= 2) {
        // Get the first submission in the list and remove it
        Submission first = remaining.get(0);
        remaining.remove(0);

        // Form a pair for every remaining submission by pairing with the first, removed submission
        for (Submission submission : remaining) {
            Pair<Submission, Submission> pair = Pair.of(first, submission);
            Pair<Submission, Submission> reversed = Pair.of(submission, first);

            // Something's wrong, we've made a duplicate pair (but reversed)
            // Should never happen
            if (pairs.contains(reversed)) {
                throw new RuntimeException("Internal error in pair generation: duplicate pair produced!");
            }

            // Add the newly-generated pair to our return
            pairs.add(pair);
        }
    }

    return pairs;
}

From source file:fr.inria.atlanmod.neoemf.core.impl.NeoEMFEObjectAdapterFactoryImpl.java

@SuppressWarnings("unchecked")
public static <T> T getAdapter(Object adaptableObject, Class<T> adapterType) {
    if (adapterType.isInstance(adaptableObject)) {
        return (T) adaptableObject;
    } else if (adapterType.isAssignableFrom(NeoEMFInternalEObject.class)
            && adaptableObject instanceof InternalEObject) {
        {/*w w w  . j a va  2 s  .c om*/
            EObject adapter = adaptedObjects.get(adaptableObject);
            if (adapter != null) {
                if (adapterType.isAssignableFrom(adapter.getClass())) {
                    return (T) adapter;
                }
            }
        }
        {
            // Compute the interfaces that the proxy has to implement
            // These are the current interfaces + NeoEMFEObject
            List<Class<?>> interfaces = new ArrayList<>();
            interfaces.addAll(ClassUtils.getAllInterfaces(adaptableObject.getClass()));
            interfaces.add(NeoEMFInternalEObject.class);
            // Create the proxy
            Enhancer enhancer = new Enhancer();
            enhancer.setClassLoader(NeoEMFEObjectAdapterFactoryImpl.class.getClassLoader());
            enhancer.setSuperclass(adaptableObject.getClass());
            enhancer.setInterfaces(interfaces.toArray(new Class[] {}));
            enhancer.setCallback(new NeoEMFEObjectProxyHandlerImpl((InternalEObject) adaptableObject));
            T adapter = (T) enhancer.create();
            adaptedObjects.put((InternalEObject) adaptableObject, (NeoEMFInternalEObject) adapter);
            return adapter;
        }
    }
    return null;
}

From source file:Main.java

/**
 * This method joins two lists returning the a single list consisting of the first followed by the second.
 * //from  w w w. j a v  a 2  s. c o m
 * @param first  first list. can be null.
 * @param second second list. can be null.
 * @param emptyResultIsNull if the result is empty, should we return null?
 * @return the concatenation of both lists or null
 */
public static <T> List<T> nullSafeAppend(List<T> first, List<T> second, boolean emptyResultIsNull) {
    List<T> result = new ArrayList<T>();

    if (first != null)
        result.addAll(first);
    if (second != null)
        result.addAll(second);

    if (result.isEmpty() && emptyResultIsNull) {
        result = null;
    }
    return result;
}

From source file:fr.inria.atlanmod.neoemf.core.impl.NeoEObjectAdapterFactoryImpl.java

@SuppressWarnings("unchecked")
public static <T> T getAdapter(Object adaptableObject, Class<T> adapterType) {
    if (adapterType.isInstance(adaptableObject)) {
        return (T) adaptableObject;
    } else if (adapterType.isAssignableFrom(InternalPersistentEObject.class)
            && adaptableObject instanceof InternalEObject) {
        {//from w  w  w .  j av a  2 s.  co m
            EObject adapter = adaptedObjects.get(adaptableObject);
            if (adapter != null) {
                if (adapterType.isAssignableFrom(adapter.getClass())) {
                    return (T) adapter;
                }
            }
        }
        {
            // Compute the interfaces that the proxy has to implement
            // These are the current interfaces + PersistentEObject
            List<Class<?>> interfaces = new ArrayList<>();
            interfaces.addAll(ClassUtils.getAllInterfaces(adaptableObject.getClass()));
            interfaces.add(InternalPersistentEObject.class);
            // Create the proxy
            Enhancer enhancer = new Enhancer();
            enhancer.setClassLoader(adaptableObject.getClass().getClassLoader());
            enhancer.setSuperclass(adaptableObject.getClass());
            enhancer.setInterfaces(interfaces.toArray(new Class[] {}));
            enhancer.setCallback(new NeoEObjectProxyHandlerImpl((InternalEObject) adaptableObject));
            T adapter = (T) enhancer.create();
            adaptedObjects.put((InternalEObject) adaptableObject, (InternalPersistentEObject) adapter);
            return adapter;
        }
    }
    return null;
}

From source file:Main.java

public static <T extends Comparable<? super T>> List<List<T>> getAllCombinations(final List<T> pList,
        final int pSize) {
    assert (pSize < pList.size());
    final List<List<T>> result = new ArrayList<List<T>>();

    if (pSize == 0) {
        result.add(new ArrayList<T>());
        return result;
    }//  w  ww .  j  a  v  a 2 s  . c o m

    final List<List<T>> combinations = getAllCombinations(pList, pSize - 1);
    for (final List<T> combination : combinations) {
        for (final T element : pList) {
            if (combination.contains(element)) {
                continue;
            }

            final List<T> list = new ArrayList<T>();
            list.addAll(combination);

            if (list.contains(element)) {
                continue;
            }

            list.add(element);
            Collections.sort(list);

            if (result.contains(list)) {
                continue;
            }

            result.add(list);
        }
    }

    return result;
}

From source file:Main.java

public static <V> List<V> uniteCollectionArray(Collection<V[]> values) {
    if (isEmpty(values)) {
        return Collections.emptyList();
    }//from  w  w w.jav  a 2 s  .  c  o  m
    List<V> copy = new ArrayList<V>();
    for (V[] collections : values) {
        copy.addAll(Arrays.asList(collections));
    }
    return copy;
}