Question

JAVA Eclipse 3.8 "id" is the primary key for Product class. I need to store a list of "Product"s in a data structure which provides fast search like provided by HashSet and also i need to get/retrieve the searched object. Using a HashMap would result in redundant "id"s which is costly.

Was it helpful?

Solution

Java's HashSet is implemented using a HashMap, so it's also going to have redundancies. If you have done performance testing and found that to be a significant performance hit consider using a high-performance collections library such as GNU Trove (specifically TIntObjectHashMap).

OTHER TIPS

If your ID is unique (no two objects has the same ID), just define properly the equals() method of your to-be-stored objects and use HashSet instead.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top