Example usage for java.util List remove

List of usage examples for java.util List remove

Introduction

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

Prototype

E remove(int index);

Source Link

Document

Removes the element at the specified position in this list (optional operation).

Usage

From source file:Main.java

public static void setByIndex() {
    List<Integer> l = new ArrayList<Integer>();
    l.add(1);/*from   w  w  w. j  a  va2s  .c o  m*/
    l.add(2);
    l.add(3);
    l.add(4);
    for (int i = 0; i < 4; i++) {
        System.out.println(l.get(0));
        l.remove(0);
    }
}

From source file:br.ufpr.inf.opla.patterns.util.AdapterUtil.java

public static List<Interface> getAllTargetInterfaces(Element adaptee) {
    List<Interface> targetInterfaces = new ArrayList<>();

    List<Element> adapteeSuperElements = ElementUtil.getAllSuperElements(adaptee);
    adapteeSuperElements.add(adaptee);//from  ww  w. ja  v a 2s  .c  o m
    List<Relationship> allRelationships = ElementUtil.getRelationships(adapteeSuperElements);
    for (Relationship relationship : allRelationships) {
        Element usedElementFromRelationship = RelationshipUtil.getUsedElementFromRelationship(relationship);
        if (usedElementFromRelationship != null && adapteeSuperElements.contains(usedElementFromRelationship)) {
            Element client = RelationshipUtil.getClientElementFromRelationship(relationship);
            if (client instanceof Class && !((Class) client).isAbstract()) {
                List<Interface> allSuperInterfaces = ElementUtil.getAllSuperInterfaces(client);
                allSuperInterfaces.remove(adaptee);
                allSuperInterfaces.removeAll(adapteeSuperElements);
                targetInterfaces = new ArrayList<>(CollectionUtils.union(targetInterfaces, allSuperInterfaces));
            }
        }
    }
    return targetInterfaces;
}

From source file:Main.java

/**
 * Checks if string representation corresponds to the collection. Parts of an expected collection should not contain
 * delimiter./*w w  w.j  a  v  a  2 s .  co m*/
 */
public static boolean isStringPermutationOfCollection(String input, Collection<String> expected,
        String delimeter, int trimFromStart, int trimFromEnd) {
    input = input.substring(trimFromStart, input.length() - trimFromEnd);
    List<String> parts = new ArrayList<>(Arrays.asList(input.split(Pattern.quote(delimeter))));
    for (String part : expected) {
        if (parts.contains(part)) {
            parts.remove(part);
        }
    }
    return parts.isEmpty();
}

From source file:Main.java

/**
 * Randomize the specified list.//from   w w  w.java2s .  c o  m
 * @param list the list to randomize.
 * @param <E> the type of elements in the list.
 * @return a new list with all the elements of the input but in a random order.
 */
public static <E> List<E> randomize(List<E> list) {
    List<E> tmp = new ArrayList<>(list);
    List<E> result = new ArrayList<>(list.size());
    while (!tmp.isEmpty()) {
        int n = rand.nextInt(tmp.size());
        result.add(tmp.remove(n));
    }
    return result;
}

From source file:Main.java

/**
 * Removes all occurrences of <code>e</code> from <code>c</code>.
 * /*w  w w  . j  a v a 2 s. c  om*/
 * @param c the <code>Collection</code>
 * @param e the element to remove
 */
public static <T> void removeAll(Collection<?> c, Object e) {
    if (c instanceof List) {
        List<?> list = (List<?>) c;

        int index = -1;
        do {
            index = list.lastIndexOf(e);
            if (index != -1)
                list.remove(index);
        } while (index != -1);

    } else if (c instanceof Set) {
        c.remove(e);
    } else {
        while (c.remove(e))
            ;
    }
}

From source file:Main.java

/**
 * Removes all null elements from list.//from  w w w.ja  v  a  2 s  . com
 *
 * @param list
 *            list to refactor
 * @param <T>
 *            list type
 * @return refactored list
 */
public static <T> List<T> removeNulls(final List<T> list) {
    if (list == null) {
        return null;
    }
    for (int i = list.size() - 1; i >= 0; i--) {
        if (list.get(i) == null) {
            list.remove(i);
        }
    }
    return list;
}

From source file:Main.java

/**
 * // ww w . j  a va  2  s .c o m
 * @param <T>
 * @param list
 */
public static <T> void filterNulls(List<T> list) {
    if (null == list || list.isEmpty())
        return;
    for (int i = 0; i < list.size(); i++) {
        T current = list.get(i);
        if (null == current) {
            list.remove(i);
        }
    }
}

From source file:Main.java

/**
 * Safety delete element included in source collection from target collection
 *
 * @param src    source collection//  w w  w. j a v a 2  s  .c  o  m
 * @param target target collection
 */
public static void safetyDeleteCollection(List src, List target) {
    int srcSize = src.size();
    for (int i = 0; i < srcSize; i++) {
        Object data2Deleted = src.get(i);
        if (target.contains(data2Deleted)) {
            target.remove(data2Deleted);
            srcSize--;
        }
    }
}

From source file:com.epsi.twitterdashboard.utils.JsonFile.java

/**
 * Add dashboard bookmark to json file//from  w w  w . j  a v a  2  s . c  o  m
 * @param id 
 */
public static void DeleteUser(int id) {
    List<User> users = ReadUsers();
    if (users == null) {
        users.remove(ListFinder.FindUserById(users, id));
        JSONArray jsonUsers = new JSONArray(users);
        Write(JsonFile.UsersPath, jsonUsers.toString());
    }
}

From source file:Main.java

/**
 * Selects pseudo-random elements from a collection
 *
 * @param c      Collection to select from
 * @param n      Number of elements to select
 * @param unique Whether the selection should be unique
 * @return Random element(s) from collection
 */// ww  w.ja v  a  2s .  c  o m
public static <E> Collection<E> randomElement(Collection<E> c, int n, boolean unique) {
    List<E> out = new ArrayList<>();
    List<E> l = new ArrayList<>(c);

    while (out.size() < n) {
        E e = l.get(ThreadLocalRandom.current().nextInt(0, l.size()));
        out.add(e);
        if (unique)
            l.remove(e);
    }
    return out;
}