3

I am using Java 7. Is it possible to get the index of an Object in an ArrayList depending on a particular attribute value. eg:

class Abc{
  String first_name;
  String last_name;
  // getter and setters...
}

Now

List<Abc> abcList = new ArrayList();
Abc abcObj = new Abc();
abcObj.setFirst_name("Jeet");
abcObj.setLast_name("Adhikari");
abcList.add(abcObj);

Abc abcObj2 = new Abc();
abcObj2.setFirst_name("John");
abcObj2.setLast_name("Something");
abcList.add(abcObj2);

Now is there any better way without iterating to get the index of the object in abcList where name = "John".

JEET ADHIKARI
  • 529
  • 2
  • 6
  • 19
  • 6
    No, unless you use a map or index. – Zelldon Aug 08 '17 at 11:01
  • 3
    You can't get an index in a list without iterating even in Java 8, where you could hide iteration behind a stream, but you wouldn't be able to get rid of it completely. – Sergey Kalinichenko Aug 08 '17 at 11:04
  • okay thanks :). Hide iteration behind a stream as in? could you please explain? – JEET ADHIKARI Aug 08 '17 at 11:07
  • 1
    What he means is using the stream api, e.g. `list.stream().filter( /*your filter*/ ).collect( /*some collector, e.g. a list collector*/ )`. – Thomas Aug 08 '17 at 11:11
  • @Thomas is Java 7. No streams available. – Juan Carlos Mendoza Aug 08 '17 at 11:13
  • @Juan Carlos Mendoza. Actually he just answered to my previous comment. – JEET ADHIKARI Aug 08 '17 at 11:15
  • Ok. But actually the advice is to hide the iteration with a stream. If it's not possible to use another data structure then I think is easier to use the indexOf. – Juan Carlos Mendoza Aug 08 '17 at 11:25
  • @JuanCarlosMendoza that was exactly his point: "You can't get an index in a list without iterating even in Java 8" - there was no advice to use streams at all but rather that even using streams in Java would not get rid of the loop. – Thomas Aug 08 '17 at 11:34
  • Btw, what's the problem with a simple loop here? If you don't write one yourself there might be some library that contains a method that implements such a loop but doing it yourself shouldn't be too hard (even in a more generic way e.g. by using Google Guava's `Predicate` etc.). - Besides that, what do you need that index for? Might there be a better option that doesn't need the index at all? – Thomas Aug 08 '17 at 11:43
  • Possible duplicate of [How to find an object in an ArrayList by property](https://stackoverflow.com/questions/17526608/how-to-find-an-object-in-an-arraylist-by-property) – Lars Aug 08 '17 at 11:56
  • You might find something useful here: https://stackoverflow.com/questions/122105/what-is-the-best-way-to-filter-a-java-collection – Lars Aug 08 '17 at 12:00

5 Answers5

5

No, but you could use a HashMap instead with first name as key and the complete object as value

HashMap<String, Abc> abcMap = new HashMap<String, Abc>();
Abc abcObj = new Abc();
abcObj.setFirst_name("Jeet");
abcObj.setLast_name("Adhikari");
abcMap.put(abcObj.getFirst_name(), abcObj);

Abc abcObj2 = new Abc();
abcObj2.setFirst_name("John");
abcObj2.setLast_name("Something");
abcMap.put(abcObj2.getFirst_name(), abcObj2);

And then you can simply get the full object by calling for e.g.

Abc objectByKey = abcMap.get("John");

I hope this alternative solution is also ok for you

Yannick
  • 813
  • 8
  • 17
3

As your list is an ArrayList, it can be assumed that it is unsorted. Therefore, there is no way to search for your element that is faster than O(n).

If you can, you should think about changing your list into a Set (with HashSet as implementation) with a specific Comparator for your sample class.

Another possibility would be to use a HashMap. You can add your data as Sample (please start class names with an uppercase letter) and use the string you want to search for as key. Then you could simply use

Sample samp = myMap.get(myKey);

If there can be multiple samples per key, use Map>, otherwise use Map<String, Sample>. If you use multiple keys, you will have to create multiple maps that hold the same dataset. As they all point to the same objects, space shouldn't be that much of a problem.

Sandip Solanki
  • 704
  • 1
  • 8
  • 15
2

There are 2 solutions:

  • Using a iteration like arrayList.indexOf(value)
  • Using a Hash based Data structure like a java.util.HashSet instead of java.util.ArrayList
1

You could use the indexOf. You must override the equals method in your Abc class.

Juan Carlos Mendoza
  • 5,736
  • 7
  • 25
  • 50
  • 4
    That's still iteration. Besides, overriding `equals()` just for that is probably not the best approach. – Thomas Aug 08 '17 at 11:08
0

I don't know the requirements behind your solution, but I would consider using a LinkedHashMap instead of the ArrayList.

yaccob
  • 1,230
  • 13
  • 16