about indexing with an object
1 posts in topic
Flat View  Flat View

Posted By:   rajiv_k
Posted On:   Friday, June 27, 2003 08:49 AM

Here's my scenario:

I have a set of objects that need to be added to a collection data structure such as an Array or a Linked List.

The objects do NOT have any primary index such as an integer, to easily distinguish between them.

Is there a data structure I can use in such a case so that each object can be retrieved in one shot, as in an array if we know the index?

Or is there some property of the object I can use to uniquely map it to an array index? I know some scripting languages provide array indexing based on strings.


Re: about indexing with an object

Posted By:   Stephen_McConnell  
Posted On:   Friday, June 27, 2003 09:52 AM

A Hashtable or a HashMap would do what you need.

You can use the object itself as a hash or some unique attribute or combination of attributes as the hash object.

Check out Hashtable (for synchronized methods) and HashMap (for unsynchronized methods) in the JavaDocs.

If you want the objects to return in an Iterator in a specific order, then you could use LinkedHashSet or LinkedHashMap.

The collections classes in java.util.* are really powerful if you begin reading the Java Tutorials and JavaDocs.

Stephen McConnell

About | Sitemap | Contact