0

I have list (array list)that can contain many instances (between 500-3000 instances ). during the program some function need to access to this list (many times)and search for specific instance or more ,to get the instance\s they need loop on the list and provide parentName and name (which is string) and are not uniqe key .

my question is since the list need to be accessed many time there is a way to define/design it better that the access to the list can be more efficient?

Please keep in mind that the functions that need to get instance/s from the list cannot provide full key the can provide only name and parentName which can have more that one instance.

List<Obj> myList = new ArrayList<Obj>(); class obj { parentName Name type curr .... 
1
  • So you should work with a map, in which each key contains a list of values. You might get a better answer if you show some more code, e.g. some example values and a snippet showing how you access the list. Commented Dec 22, 2012 at 11:04

1 Answer 1

3

Use a Map<MyEntry, List<Obj>> where MyEntry is a class enclosing parent name and name as such:

public final class MyEntry { private final String parentName; private final String name; private final int hashCode; public MyEntry(final String parentName, final String name) { this.parentName = parentName; this.name = name; hashCode = 31 * parentName.hashCode() + name.hashCode(); } // Override .equals() and .hashCode() @Override public int hashCode() { return hashCode; } @Override public boolean equals(final Object o) { if (this == o) return true; if (o == null) return false; if (getClass() != o.getClass()) return false; final MyEntry other = (MyEntry) o; return parentName.equals(other.parentName) && name.equals(other.name); } // Have a nice string representation @Override public String toString() { return "parent name: " + parentName + ", name: " + name; } } 

You can, for instance, have a method in your Obj which returns the matching MyEntry object. Also, if you use Guava, have a look at MultiMap.

You will notice that the hash code is precomputed: this can be done since the MyEntry class is immutable. This allows for very fast usage as keys for a Map.

(edit: added .toString())

Sign up to request clarification or add additional context in comments.

8 Comments

Hi Fge,let me see if i got u right .you are telling me to create new class which will contain key for e.g. parent name 1 name 2 and in the list they have 10 instances the entry will be 1 & 2 and they map to list with 10 instances and etc?
@BorisMandovskes: yes, exactly. Your map will have entries like ("foo", "bar") -> [ obj1, obj2, etc] where obj1 and others will have "foo" as parent name and "bar" as name. Also note that it would be a good idea to override toString() in MyEntry, but that is up to you.
Hi Fge thanks for your answer,can you just explain briefly what is the purpose of the hash and equals. thanks!!!
@BorisMandovskes: the key set of a Map is a Set. Unlike a List, elements in Set are unique. You will notice that the .add() method of Set returns a boolean, which is true if and only if the set was modified as the result of the operation. In order to detect that elements are equal, implementations first check the hash code, and if an element has the same hash code of elements already present in the set, only then they use .equals(). As the MyEntry class as designed is immutable, we can calculate the hash code beforehand and make the operation all the faster.
Hi again one last question :),since i use it like <key,list<obj>> why dont use hashMap ? Thanks!!!
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.