Java List Contain containsInstance(List list, Object object)

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

Description

Returns true if the list contains the exact instance of the specified object.

License

Mozilla Public License

Parameter

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

Return

True if the object was found.

Declaration

public static boolean containsInstance(List<?> list, Object object) 

Method Source Code

//package com.java2s;
/**//from  w ww  .  jav a  2s  .  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 {
    /**
     * Returns true if the list contains the exact instance of the specified object.
     * 
     * @param list List to search.
     * @param object Object instance to locate.
     * @return True if the object was found.
     */
    public static boolean containsInstance(List<?> list, Object object) {
        return indexOfInstance(list, object) > -1;
    }

    /**
     * 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. containsIndex(int index, List objects)
  2. containsInEach(List first, List second)
  3. ContainsInList(String list, String value)
  4. containsInOrder(List a, List b)
  5. containsInstance(final List list, final Class clazz)
  6. containsItems(List smallerList, List largerList)
  7. containsNonPositives(List vals)
  8. containsNull(List list)
  9. containsNullElement(List source)