Java Java Data Structures Efficiency! Design the UI

Andrea Miotto
Andrea Miotto
iOS Development Techdegree Graduate 23,357 Points

Set, HashSet, Map, HashMap, TreeSet, TreeMap

Can someone explain me the main difference between these interfaces and their implementations?

Thank you!

1 Answer

Hello Andrea Miotto,

This is a good question as Datatype are very important in all programming languages and not only in Java.

Lets go over what these items are

Set: A Set is a Collection that cannot contain duplicate elements. It models the mathematical set abstraction.

HashSet: HashSet extends AbstractSet and implements the Set interface. It creates a collection that uses a hash table for storage.

Map: The Map interface maps unique keys to values. A key is an object that you use to retrieve a value at a later date.

HashMap: The HashMap class uses a hashtable to implement the Map interface. This allows the execution time of basic operations, such as get( ) and put( ), to remain constant even for large sets.

TreeSet: TreeSet provides an implementation of the Set interface that uses a tree for storage. Objects are stored in sorted, ascending order. Access and retrieval times are quite fast, which makes TreeSet an excellent choice when storing large amounts of sorted information that must be found quickly.

TreeMap: The TreeMap class implements the Map interface by using a tree. A TreeMap provides an efficient means of storing key/value pairs in sorted order, and allows rapid retrieval. You should note that, unlike a hash map, a tree map guarantees that its elements will be sorted in ascending key order.

For Clarification a hash table: A hash table stores information by using a mechanism called hashing. In hashing, the informational content of a key is used to determine a unique value, called its hash code. The hash code is then used as the index at which the data associated with the key is stored. The transformation of the key into its hash code is performed automatically

Hope this helps!

Ray Wai
Ray Wai
2,355 Points

Why are you saying this as datatypes? Aren't datatypes referring to those like 'char', 'int', 'String' ?

Faisal Julaidan
Faisal Julaidan
13,944 Points

this is really helpful, thanks a lot.