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

13,105

Solution 1

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;
    }
}

Solution 2

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);

Solution 3

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");

Solution 4

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);

Share:
13,105

Related videos on Youtube

Rifat
Author by

Rifat

Love math but bad in math. Love friendship, money and fame too.

Updated on June 04, 2022

Comments

  • Rifat
    Rifat almost 2 years

    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?

    • SteelToe
      SteelToe about 6 years
      You want the index of an element, or an element from a index?
    • Selvin
      Selvin about 6 years
      Even if there would be no such method still you could write own one (which you should learn on algorithms basics)
    • Rifat
      Rifat about 6 years
      index with its value.
    • Selvin
      Selvin about 6 years
  • Jakob Probst
    Jakob Probst about 6 years
    I‘d rather use a normal for-loop than a foreach-loop like you did for this example, you do not have to worry about any external variable and it’s more compact.