johnnesto
johnnesto

Reputation: 41

How to find the index of matching string in a list java?

Lets say there's an array list student of strings like

["name"],
["email"],
["address:]

Is there a way to find the index position of list which contains the matching string address
list.indexOf() only returns the value if the exact match of string is found. In this case, student.indexOf("address") returns -1 since the exact match is not found. I am aware of contains() method but it only tells if the required string is present or not. It doesn't return the index position.

Upvotes: 1

Views: 7078

Answers (4)

WJS
WJS

Reputation: 40057

You could do it like this. Just create a method, getIndex() that takes the partial string and list and returns the index.

List<String> strings = List.of("name", "email", "address:");
String[] testData = { "ame", "ema", "add", "foo" };

for (String data : testData) {
    int i = getIndex(data, strings);
    System.out.println(i >= 0 ?
            (strings.get(i) + " found for '" + data + "'") :
            "No match for '" + data + "'");
}

prints

name found for 'ame'
email found for 'ema'
address: found for 'add'
No match for 'foo'

And here is the method. Simply continuing taking items until the string contains the substring. The returned count is then the actual index of that string in the list. An extra test is done to return -1 if not found.

public static int getIndex(String s, List<String> list) {
    int index = (int) list.stream()
            .takeWhile(str -> !str.contains(s)).count();
    return index < list.size() ? index : -1;
}

Note that if two strings have the same substring, the index of the first encountered will be returned.

Upvotes: 0

user16669986
user16669986

Reputation:

not sure if i got you

but is that what you looking for

ArrayList<String> students = new ArrayList<>();
    students.add("[\"name\"],[\"email\"],[\"addrss:]");
    students.add("[\"name\"],[\"email\"],[\"addess:]");
    students.add("[\"name\"],[\"email\"],[\"address:]");

    System.out.println(students.stream()
            .filter(s -> s.contains("address"))
            .limit(1)
            .map(students::indexOf)
            .findFirst().orElse(-1));

this will get you the index of the first occurrence same as indexOf do

Upvotes: 1

Hpn4
Hpn4

Reputation: 119

You can use a ArrayList<String> to do that. This class contains a method called indexOf(). To instantiate a ArrayList you can do :

ArrayList<String> students = new ArrayList<>();
students.add("name");
students.add("email");
students.add("address");

int index = students.indexOf("address");

But if you don't want to use a List object, you can loop through your array with a simple for loop and check with a condition like this :

int index = -1;
for(int i = 0; i < students.length; i++)
    if(students[i].equals("address"))
        index = i;

Upvotes: 1

ave4496
ave4496

Reputation: 3028

You can search for all strings containing the search value and then call the indexOf method on every result object.

List<String> values = Arrays.asList("abc", "def", "ghi", "a", "ab");

List<Integer> indexes = values.stream().filter(v -> v.contains("ab")).map(v -> values.indexOf(v))
            .collect(Collectors.toList());

System.out.println(indexes); // prints [0, 4]

If you just need the first index you can do

Integer firstIndex = values.stream().filter(v -> v.contains("ab")).map(v -> values.indexOf(v)).findFirst()
                .orElse(-1);
System.out.println(firstIndex); // prints 0

Upvotes: 4

Related Questions