Example usage for java.util List indexOf

List of usage examples for java.util List indexOf

Introduction

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

Prototype

int indexOf(Object o);

Source Link

Document

Returns the index of the first occurrence of the specified element in this list, or -1 if this list does not contain the element.

Usage

From source file:Main.java

/**
 * @param string - this is the current value, e.g. itemNo
 * @param list - list of strings, e.g. list of products
 * @return string value//from  w ww.ja  v  a 2s.c  om
 * method = getPreviousInList
 * Send in value and a list of values, returns the previous value from the list.
 * returns null if the value sent in is the first value in the list.
 * 
 */
public static String getPreviousInList(String string, List list) {
    String previousString = null;
    if (list != null) {
        int i = list.indexOf(string);
        if (i == 0 || list.size() < 2) {//this is the first value in the list
            return previousString;
        } else {
            previousString = list.get(i - 1).toString();
        }
    }
    return previousString;
}

From source file:Main.java

/**
 * Corresponds to the pos() xpath function
 * /*w  w w .  j a  v  a 2s. c  o m*/
 * @param e
 * @return pos(), also the start index is 1 not 0
 */
public static int getPosition(Element e) {
    Element parent = (Element) e.getParentNode();
    List<Element> children = getChildElements(parent);
    return children.indexOf(e) + 1;
}

From source file:Main.java

public static <T> List range(List<? extends T> list, T min, T max, Comparator<? super T> comparator) {
    List<? super T> copyList = new ArrayList<T>(list);
    Collections.sort(list, comparator);
    if (copyList.indexOf(min) < copyList.indexOf(max)) {
        return copyList.subList(copyList.indexOf(min), copyList.indexOf(max));
    } else {//from   w ww .  ja va 2 s  .  c  om
        return newArrayList();
    }
}

From source file:Main.java

/**
 * This method does the same as {@link #moveLeft(int, Object, List)} but it moves the specified element
 * to the right instead of the left.//w  w  w.  j a v  a2s . c  o  m
 */
public static <T> List<T> moveRight(int offset, T element, List<T> list) {
    final int elementIndex = list.indexOf(element);

    if (elementIndex == -1) {
        throw new NoSuchElementException("Element not found in provided list.");
    }

    if (offset == 0) {
        return list;
    } else {
        int newElementIndex = elementIndex + offset;

        // Ensure that the element will not move off the end of the list.
        if (newElementIndex >= list.size()) {
            newElementIndex = list.size() - 1;
        } else if (newElementIndex < 0) {
            newElementIndex = 0;
        }

        List<T> result = new ArrayList<>(list);
        result.remove(element);

        result.add(newElementIndex, element);

        return result;
    }
}

From source file:Main.java

public static <T> List<T> makeItemFirst(final List<T> items, T item) {
    List<T> newItems = new LinkedList<T>();
    for (int i = items.indexOf(item); i < items.size(); i++) {
        newItems.add(items.get(i));/*  w w  w .ja  v a 2 s  . c  o m*/
    }

    for (int i = items.indexOf(item) - 1; i >= 0; i--) {
        newItems.add(items.get(i));
    }

    return newItems;
}

From source file:Main.java

public static <S> Map<Integer, S> asIndexMap(List<S> pList) {
    final HashMap<Integer, S> map = new HashMap<Integer, S>(pList.size());
    for (final S s : pList) {
        map.put(pList.indexOf(s), s);
    }/*from  www  .  j a v  a 2s  .  c om*/
    return map;
}

From source file:Main.java

public static <T> List range(List<? extends T> list, T min, T max) {
    List<? super T> copyList = new ArrayList<>(list);
    Collections.sort(copyList, (o1, o2) -> Integer.compare(o1.hashCode(), o2.hashCode()));
    if (copyList.indexOf(min) < copyList.indexOf(max)) {
        return copyList.subList(copyList.indexOf(min), copyList.indexOf(max));
    } else {/*from  ww w  . j  a  v a  2 s. c  o  m*/
        return newArrayList();
    }
}

From source file:Main.java

public static <T> Object getContent(List<T> collection, Object object) {
    Object retValue = null;/*from w ww  .jav  a  2s  .c o  m*/
    if (collection != null && !collection.isEmpty() && collection.contains(object)) {
        retValue = collection.get(collection.indexOf(object));
    }
    return retValue;
}

From source file:com.aliyun.openservices.odps.console.commands.HtmlModeCommand.java

/**
 * ??command/*from   w ww .j  a v  a  2 s  .  c  o  m*/
 * **/
public static HtmlModeCommand parse(List<String> optionList, ExecutionContext sessionContext) {

    if (optionList.contains("--html")) {

        optionList.remove(optionList.indexOf("--html"));
        return new HtmlModeCommand("--html", sessionContext);
    }
    return null;
}

From source file:Main.java

/**
 * Append a child element to the parent at the specified location.
 *
 * Starting with a valid document, append an element according to the schema
 * sequence represented by the <code>order</code>. All existing child
 * elements must be include as well as the new element. The existing child
 * element following the new child is important, as the element will be
 * 'inserted before', not 'inserted after'.
 *
 * @param parent parent to which the child will be appended
 * @param el     element to be added//from www.  j av a 2 s .  c  om
 * @param order  order of the elements which must be followed
 * @throws IllegalArgumentException if the order cannot be followed, either
 *                                  a missing existing or new child element
 *                                  is not specified in order
 *
 * @since 8.4
 */
public static void appendChildElement(Element parent, Element el, String[] order)
        throws IllegalArgumentException {
    List<String> l = Arrays.asList(order);
    int index = l.indexOf(el.getLocalName());

    // ensure the new new element is contained in the 'order'
    if (index == -1) {
        throw new IllegalArgumentException(
                "new child element '" + el.getLocalName() + "' not specified in order " + l); // NOI18N
    }

    List<Element> elements = findSubElements(parent);
    Element insertBefore = null;

    for (Element e : elements) {
        int index2 = l.indexOf(e.getLocalName());
        // ensure that all existing elements are in 'order'
        if (index2 == -1) {
            throw new IllegalArgumentException(
                    "Existing child element '" + e.getLocalName() + "' not specified in order " + l); // NOI18N
        }
        if (index2 > index) {
            insertBefore = e;
            break;
        }
    }

    parent.insertBefore(el, insertBefore);
}