tags:

views:

96

answers:

7

Hello :),

is it possible to determine the ArrayList-index of an element inside the ArrayList based on one of its attributes?

In my particular case I have an Object "worker" with the attributes "name" and "worker_id". The worker_id is an ID I get from the database.

Since the first worker-object inside the ArrayList doesn't necessarily start with 0 (and may have gaps), it's not the same ID as the index each element has inside this ArrayList.

Now I want to determine this particular index of a worker-object inside the ArrayList based on its attribute "worker_id". However: since the list can be very long, I'd like to not have to iterate over the worker-ArrayList everytime.

Using a HashMap might be a solution, but I'd like to use an ArrayList if possible, because I normally won't need a key (except in this case).

Any suggestions are welcome!

Thank you in advance, Igor.

A: 

You need to check every object for it's id. There is no direct way i guess. Iterate through all the objects and check for the id.

Dheeraj Joshi
+1  A: 

You may consider using a LinkedHashMap. This offers the best of both worlds: it can behave as a list due to its predictable iteration order and it offers fast (O(1)) key based lookup at the same time.

The price is, as can be expected, a slight performance penalty (compared to ArrayList).

Péter Török
I'll look into this. However: I am using Google Web Toolkit and therefore it might not be implemented in there - I'll have to check. I'd prefer to keep the ArrayList though. Thank you very much for your answer though :).
Igor
+3  A: 

Since the first worker-object inside the ArrayList doesn't necessarily start with 0 (and may have gaps), it's not the same ID as the index each element has inside this ArrayList.

It sounds to me that the list is at least sorted? In that case, you could use a binary search, which is slightly faster than a linear scan of the list.

Another option is to define two workers to be equal if they have the same worker_id. Then you could simply do int index = workerList.indexOf(new Worker(idToSearchFor));

Using a HashMap might be a solution, but I'd like to use an ArrayList if possible, because I normally won't need a key (except in this case).

Well, to be honest, when I use a map it is seldom because I couldn't do it with a list, but rather because the interface of a map is nicer to work with, and results in cleaner code. So, even though you may think that a HashMap is an overkill, it may be worth it, solely because it's slightly more logical and the code ends up being a bit easier to read.

aioobe
Actually all of the solutions posted here are interesting. I try to overwrite equal and use indexOf with some sort of "dummy" worker-object, that only contains a valid worker_id. Thanks for all other suggestions though :).
Igor
A: 

Something like:

List<Worker> workers = // ...
for (int i = 0; i < workers.size(); i++) {
    if (/* ... */) {
        return i;
    }
}
Vivien Barousse
That's something I came across too, but thought there must be a smarter solution than just iterating hundrets of times through the list.
Igor
A: 

int find(List <A> list, Object sth) { Iterator it = list.iterator(); int index = 0; while(it.hasNext()); A current = it.next(); if (el.getSth().equals(element)) { return index; } index++; } return -1; }

Gadolin
A: 

Create the List as big as the largest worker_id value. while inserting the object, insert it at worker_id number. like

list.add(obj.worker_id,obj);
while retrieving
list.get(obj.worker_id);

yes, its not space effecient solution, the space efficient solution is to use the Hashmap (or its variant) as everyone else have already mentioned.

Dhana
A: 

I think you have two choices:

  1. Build an index. A hash is a decent mechanism for accessing an index, but there are others. A simple sparse array might work in this context, for example.
  2. Search for the matching element. A linear scan is O(n) but a binary search is O(log n) so will be better if your list is sorted (assuming any non-trivial length).
Bill Michell