Rifat
Rifat

Reputation: 1888

Java - How to get item index from an array with its value?

If I have a array list say a string array, then how can I get index of an element with its value?

Code exammple:

    private static final String[] TEST = new String[]{
      "abc","bcd","def"
}

require its index with something like:

       int i = TEST.getPosition("abc");/TEST.indexof(...);//not available

Also I tried with creating ArrayAdapter.

Is there anything like this? Or maybe I need to assign it with that adapter or something?

Or,

How can I get such index of an element?

Upvotes: 2

Views: 15168

Answers (4)

Tung Tran
Tung Tran

Reputation: 2955

Simplest idea:

Convert array to list and you can get position of an element.

List<String> abcd  = Arrays.asList(yourArray);
int i = abcd.indexOf("abcd");

Other solution, you can iterator array:

int position = 0;
for (String obj : yourArray) {
    if (obj.equals("abcd") {
       return position;
    }
    position += 1;
} 

//OR
for (int i = 0; i < yourArray.length; i++) {
    if (obj.equals("abcd") {
       return i;
    }
}

Upvotes: 5

shmosel
shmosel

Reputation: 50716

As others have said, the simplest method is Arrays.asList(TEST).indexOf("abc"). But here's an alternative using streams:

int index = IntStream.range(0, TEST.length)
        .filter(i -> TEST[i].equals("abc"))
        .findFirst()
        .orElse(-1);

Upvotes: 1

Jitindra Fartiyal
Jitindra Fartiyal

Reputation: 117

There can be two methods which you can opt for -

1) First, as it is an array, you can simply run a for loop and compare using equals() and find the position.

2) Secondly, convert it into an List<> and run indexOf() and find the position.

NOTE: When you convert an array to List, it become a backed version List, it is fixed and size can't be alter --> but it is perfectly fine in your case as your array is final.

List<String> str = Arrays.asList(TEST);
int pos = str.indexOf("abc");

Upvotes: 1

Nicolas
Nicolas

Reputation: 8650

You can refer to this question and use the Java 8 Lambda filter function.

Optional<String> optional = Arrays.stream(presents)
                               .filter(x -> "MyString".equals(x.getName()))
                               .findFirst();

if(optional.isPresent()) {//Check whether optional has element you are looking for
    String s = optional.get();//get it from optional
}

the filter function take a lambda callback and will returns a list with only the values that match the condition in the lambda. In our case, we also want only the first one. That's why are add the findFirst() which will returns only the first value.
The function will returns an Optional, we can check if it contains a value using options.isPresent().
The optional.get() will returns the value.
Replace the "MyString" by your value and it should work.

If you wish to find an index, you can use the indexOf() function after changing your static array to an list : Arrays.asList(array);

Upvotes: 1

Related Questions