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.

有帮助吗?

解决方案

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).

其他提示

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.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top