Java Collection Search indexOf(Collection stringCollection, String str)

Here you can find the source of indexOf(Collection stringCollection, String str)

Description

Returns index of string in a string collection

License

Open Source License

Parameter

Parameter Description
stringCollection collection of strings
str string to search for

Return

index of the string if found, otherwise -1

Declaration

static public int indexOf(Collection<String> stringCollection, String str) 

Method Source Code

//package com.java2s;
/*******************************************************************************
* Copyright (c) 2015 ARM Ltd. and others
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:/*  w  w  w  .  j a  va2 s  .  co  m*/
* ARM Ltd and ARM Germany GmbH - Initial API and implementation
*******************************************************************************/

import java.util.Collection;

public class Main {
    /**
     * Returns index of string in a string collection 
     * @param stringCollection collection of strings 
     * @param str string to search for
     * @return index of the string if found, otherwise -1 
     */
    static public int indexOf(Collection<String> stringCollection, String str) {
        if (str != null && stringCollection != null) {
            int i = 0;
            for (String s : stringCollection) {
                if (s.equals(str)) {
                    return i;
                }
                i++;
            }
        }
        return -1;
    }

    /**
     * Returns index of string in a string array 
     * @param stringArray collection of strings 
     * @param str string to search for
     * @return index of the string if found, otherwise -1 
     */
    static public int indexOf(String[] stringArray, String str) {
        if (str != null && stringArray != null) {
            int i = 0;
            for (String s : stringArray) {
                if (s.equals(str)) {
                    return i;
                }
                i++;
            }
        }
        return -1;
    }
}

Related

  1. hasNoValue(Collection collection)
  2. hasOneItem(final Collection col)
  3. hasOtherThan(Collection a, Collection b)
  4. hasValidElement(Collection collection)
  5. hasValue(Collection collection)
  6. indexOf(final Collection c, final Object elem)
  7. indexOfInstance(final Collection collection, final T instance)
  8. indexOfObjectIdentity(Collection collection, Object object)