Best datastructure for frequently queried list of objects
Posted
by panzerschreck
on Stack Overflow
See other posts from Stack Overflow
or by panzerschreck
Published on 2010-05-07T00:47:47Z
Indexed on
2010/05/07
0:58 UTC
Read the original article
Hit count: 233
Hello,
I have a list of objects say, List. The Entity class has an equals method,on few attributes ( business rule ) to differentiate one Entity object from the other.
The task that we usually carry out on this list is to remove all the duplicates something like this :
List<Entity> noDuplicates = new ArrayList<Entity>();
for(Entity entity: lstEntities)
{
int indexOf = noDuplicates.indexOf(entity);
if(indexOf >= 0 )
{
noDuplicates.get(indexOf).merge(entity);
}
else
{
noDuplicates.add(entity);
}
}
Now, the problem that I have been observing is that this part of the code, is slowing down considerably as soon as the list has objects more than 10000.I understand arraylist is doing a o(N) search.
Is there a faster alternative, using HashMap is not an option, because the entity's uniqueness is built upon 4 of its attributes together, it would be tedious to put in the key itself into the map ? will sorted set help in faster querying ?
Thanks
© Stack Overflow or respective owner