Java List IndexOf indexOfInstance(List list, Object object)

Here you can find the source of indexOfInstance(List list, Object object)

Description

Performs a lookup for the exact instance of an object in the list and returns its index, or -1 if not found.

License

Mozilla Public License

Parameter

Parameter Description
list List to search.
object Object instance to locate.

Return

Index of the object.

Declaration

public static int indexOfInstance(List<?> list, Object object) 

Method Source Code

//package com.java2s;
/**/*from ww w .  j a v  a  2 s .  c  o  m*/
 * This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0. 
 * If a copy of the MPL was not distributed with this file, You can obtain one at 
 * http://mozilla.org/MPL/2.0/.
 * 
 * This Source Code Form is also subject to the terms of the Health-Related Additional
 * Disclaimer of Warranty and Limitation of Liability available at
 * http://www.carewebframework.org/licensing/disclaimer.
 */

import java.util.List;

public class Main {
    /**
     * Performs a lookup for the exact instance of an object in the list and returns its index, or
     * -1 if not found. This is different from the usual implementation of a list search the uses
     * the object's equals implementation.
     * 
     * @param list List to search.
     * @param object Object instance to locate.
     * @return Index of the object.
     */
    public static int indexOfInstance(List<?> list, Object object) {
        for (int i = 0; i < list.size(); i++) {
            if (list.get(i) == object) {
                return i;
            }
        }

        return -1;
    }
}

Related

  1. indexOfFactory(List factories, Class classInstance)
  2. indexOfId(List aList, Object anObj)
  3. indexOfIdentical(List list, Object value)
  4. indexOfIdentity(List l, T t)
  5. indexOfIgnoreCase(List values, String target)
  6. indexOfInstance(List list, Class type)
  7. indexOfMax(List list)
  8. indexOfMax(List list)
  9. indexOfMinSize(final List> sets)