Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

java: maps zoo, what to choose

I'm pretty new to the Java World (since I'm writing primary in C/C++). I'm using maps in my apps. Since java.util.Map is abstract I need to instantiate it's implementation. Usually I use HashMap like:

Map<String, MyClass> x = new HashMap<>();

But in java docs I found many other implementations, like TreeMap, LinkedHashMap, HashTable, etc. I want to know if I can continue blindly using of the HashMap or there are any important differences between those Map implementations.

The brief list of points-to-know will be ok. Thanks.

like image 278
maverik Avatar asked Dec 20 '12 14:12

maverik


People also ask

What type of map should I use Java?

General-Purpose Map Implementations If you need SortedMap operations or key-ordered Collection -view iteration, use TreeMap ; if you want maximum speed and don't care about iteration order, use HashMap ; if you want near- HashMap performance and insertion-order iteration, use LinkedHashMap .

Which map is more efficient in Java?

TreeMap in Java 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.

What is the class for map in Java?

The three primary classes that implement map in Java are: HashMap. LinkedHashMap. TreeMap.

What is Java map value?

HashMap values() Method in Java values() method of HashMap class in Java is used to create a collection out of the values of the map. It basically returns a Collection view of the values in the HashMap. Syntax: Hash_Map.values() Parameters: The method does not accept any parameters.


2 Answers

  • Never bother with Hashtable, it's a relic from Java 1.0;
  • HashMap is the universal default due to O(1) lookup and reliance only on equals and hashCode, guaranteed to be implemented for all Java objects;
  • TreeMap gives you sorted iteration over the map entries (plus a lot more—see NavigableMap), but requires a comparison strategy and has slower insertion and lookup – O(logN) – than HashMap;
  • LinkedHashMap preserves insertion/access order when iterating over the entries.

SortedMap implementations offer some great features, like headMap and tailMap. NavigableMap implementations offer even more features with terrific performance for operations that assume sorted keys.

Further out there are java.util.concurrent map implementations, like ConcurrentHashMap, which offer great concurrent performance and atomic get/put operations.

like image 181
Marko Topolnik Avatar answered Oct 16 '22 18:10

Marko Topolnik


  • HashMap use it almost all the time. Note that your object need have proper implementation of equals and hashCode methods. Does not save insertion order.
  • HashTable don't use it never.
  • LinkedHashMap the same as HashMap but saves insertion order. Large overhead.
  • TreeMap support natural ordering. But insertion works in O(logn).
like image 31
mishadoff Avatar answered Oct 16 '22 20:10

mishadoff