Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Collection with index and hash access

I need a collection class which has both: quick index and hash access. Now I have ArrayList. It has good index acces, but his contains method is not performant. HashSet has good contains implementation but no indexed acces. Which collection has both? Probably something from Apache? Or should I create my own collection class which has both: ArrayList for indexed acces and HashSet for contains check?

Just for clarification: i need both get(int index) and contains(Object o)

like image 603
Sergiy Medvynskyy Avatar asked Feb 22 '13 11:02

Sergiy Medvynskyy


People also ask

What is hash indexing?

A hash index organizes the search keys with their associated pointers into a hash file structure. We apply a hash function on a search key to identify a bucket, and store the key and its associated pointers in the bucket (or in overflow buckets).

How hash based indexing is implemented?

Hashing technique is used to calculate the direct location of a data record on the disk without using index structure. In this technique, data is stored at the data blocks whose address is generated by using the hashing function. The memory location where these records are stored is known as data bucket or data blocks.

Why there is no get method in HashSet?

HashSet does not have any method to retrieve the object from the HashSet. There is only a way to get objects from the HashSet via Iterator. When we create an object of HashSet, it internally creates an instance of HashMap with default initial capacity 16.

What is the search key in hash index?

Search Key - attribute to set of attributes used to look up records in a file. – Hash indices: search keys are distributed uniformly across “buckets” using a “hash function”. – or records with an attribute value falling in a specified range of values.


1 Answers

If indexed access performance is not a problem the closest match is LinkedHashSet whose API says that it is

Hash table and linked list implementation of the Set interface, with predictable iteration order.

at least I dont think that the performance will be be worse than that of LinkedListPerformance. Otherwise I cannot see no alternative but your ArrayList + HashTable solution

like image 81
Evgeniy Dorofeev Avatar answered Oct 19 '22 17:10

Evgeniy Dorofeev