Better data structure for a frequently requested list of objects

I have a list of objects like List. The Entity class has an equals method, by several attributes (business rule), to distinguish one Entity from another.

The task that we usually perform on this list is to remove all duplicates:

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 observed is that this part of the code slows down significantly as soon as the list has objects over 10,000. I understand that the arraist searches o (N).

Is there a faster alternative, using HashMap is not an option, because the uniqueness of an entity is built on 4 of its attributes together, would it be tiring to insert the key itself into the map? will sort the help given in a faster request?

thank

+5
6

, , , , 10000. , o (N).

, O (N)

  • lstEntities - O (N)
  • ArrayList.indexOf(T), - O (N)

, , O (N ^ 2), .

, , :

  • List
  • , "" .

, , . Entity, , "" Entity, , ID, , , getID(), , , , "" . :

Map<ID, Entity> map = new HashMap<ID, Entity>(inputList.size());
for (Entity e : inputList) {
    Entity existing = map.get(e.getID());
    if (existing == null) {
        //not in map, add it
        map.put(e.getID(), e);
    } 
    else {
        existing.merge(e);
    }
}

- O (n), HashMap.get(K) - .

+2

( , Entity), . , , TreeSet Comparator Entity . ( ) , .

+3

Set List, Set . , List Set

List<Entity> list = //your list.
Set<Entity> set = new HashSet<Entitiy>();
set.addAll(list);

, , - List ? , Set - .

Set ( List, get(int index)). Set .

, . / , get(int index) remove(int index) - , Set .

+2

, merge. merge , , equals? , , , :

hashCode Entity, equals. :

public int hashCode() {
  // assuming the four attributes that determine equality are called
  // attrFoo, attrBar, attrBaz, and attrQux
  int hash = 1;
  hash += attrFoo == null ? 0 : attrFoo.hashCode();
  hash *= 37;
  hash += attrBar == null ? 0 : attrBar.hashCode();
  hash *= 37;
  hash += attrBaz == null ? 0 : attrBaz.hashCode();
  hash *= 37;
  hash += attrQux == null ? 0 : attrQux.hashCode();

  return hash;
}

HashMap, :

Map<Entity, Entity> map = new HashMap<Entity, Entity>();
for(Entity entity: lstEntities) {
  if (map.containsKey(entity)) {
    map.get(entity).merge(entity);
  } else {
    map.put(entity, entity);
  }
}
return map.values();  // or keys().  Whichever.

, , , Map , , , , , .

+1

, , , Set - , HashSet.

-, " 4 " , . hashcode(), equals(), Set, .

0

O (N * Log (N)):

  • Iterate over the list, comparing each element with the next in the list, if they are equal, combine them and delete.
0
source

All Articles