HashMap Class

HashMap is a Hash table based implementation of the Map interface. For more details on hashing, you can refer to http://javajee.com/hashing-basics.

 

Important properties of HashMap

  1. Provides all of the optional map operations

  2. Permits null values and the null key.

  3. Roughly equivalent to Hashtable, except that it is unsynchronized and permits nulls.

  4. Makes no guarantees regarding order of the map

  5. Provides constant-time performance for the basic operations (get and put)

    • assuming the hash function disperses the elements properly among the buckets.

  6. ​An instance of HashMap has two parameters that affect its performance:

    • initial capacity and load factor.

      • The capacity is the number of buckets in the hash table, and the initial capacity is simply the capacity at the time the hash table is created.

      • The load factor is a measure of how full the hash table is allowed to get before its capacity is automatically increased.

    • When the number of entries in the hash table exceeds the product of the load factor and the current capacity, the hash table is rehashed (that is, internal data structures are rebuilt) so that the hash table has approximately twice the number of buckets.

    • Chosing right values for capacity and load factor are important:

      • The expected number of entries in the map and its load factor should be taken into account when setting its initial capacity, so as to minimize the number of rehash operations.

      • If the initial capacity is greater than the maximum number of entries divided by the load factor, no rehash operations will ever occur.

        • If many mappings are to be stored in a HashMap instance, creating it with a sufficiently large capacity will allow the mappings to be stored more efficiently than letting it perform automatic rehashing as needed to grow the table.

      • Higher values decrease the space overhead but increase the lookup cost (reflected in most of the operations of the HashMap class, including get and put).

      • In general, don't set the initial capacity too high (or the load factor too low).

      • ​The default load factor (.75) offers a good tradeoff between time and space costs.

  7. is not synchronized:

    • If multiple threads access a hash map concurrently, and at least one of the threads modifies the map structurally, it must be synchronized externally

    • Could synchronize on some object that naturally encapsulates the map.

    • Else, map could be "wrapped" using the Collections.synchronizedMap method. This is best done at creation time, to prevent accidental unsynchronized access to the map:

  8. The iterators returned by all collection view methods" are fail-fast:

    1. fails quickly and cleanly, rather than risking undetermined behaviour in the future.

    2. if the list is structurally modified at any time after the iterator is created, except through the Iterator's own remove or add methods, the iterator will throw a ConcurrentModificationException.

    3. Fail-fast iterators throw ConcurrentModificationException on a best-effort basis and cannot be guaranted; so we should not depend on it while coding.

 

Method Summary

HashMap provides all of the optional map operations. 

Additional important methods include clone(), which returns a shallow copy of this HashMap instance: the keys and values themselves are not cloned.

References and notes: 

A structural modification is any operation that adds or deletes one or more mappings; merely changing the value associated with a key that an instance already contains is not a structural modification.

https://docs.oracle.com/javase/8/docs/api/java/util/HashMap.html

Quick Notes Finder Tags

Activities (1) advanced java (1) agile (3) App Servers (6) archived notes (2) ArrayLists (1) Arrays (2) Best Practices (12) Best Practices (Design) (3) Best Practices (Java) (7) Best Practices (Java EE) (1) BigData (3) Chars & Encodings (6) coding problems (2) Collections (15) contests (3) Core Java (All) (53) course plan (2) Database (12) Design patterns (8) dev tools (3) downloads (2) eclipse (9) Essentials (1) examples (14) Exception (1) Exceptions (4) Exercise (1) exercises (6) Getting Started (18) Groovy (2) hadoop (4) hibernate (77) hibernate interview questions (6) History (1) Hot book (4) http monitoring (2) Inheritance (4) intellij (1) java 8 notes (4) Java 9 (1) Java Concepts (7) Java Core (7) java ee exercises (1) java ee interview questions (2) Java Elements (16) Java Environment (1) Java Features (4) java interview points (4) java interview questions (4) javajee initiatives (1) javajee thoughts (3) Java Performance (6) Java Programmer 1 (11) Java Programmer 2 (8) Javascript Frameworks (1) Java SE Professional (1) JSP (1) Legacy Java (1) linked list (3) maven (1) Multithreading (16) NFR (1) No SQL (1) Object Oriented (9) OCPJP (4) OCPWCD (1) OOAD (3) Operators (4) Overloading (2) Overriding (2) Overviews (1) policies (1) programming (1) Quartz Scheduler (1) Quizzes (17) RabbitMQ (1) references (2) resources (1) restful web service (3) Searching (1) security (10) Servlets (8) Servlets and JSP (31) Site Usage Guidelines (1) Sorting (1) source code management (1) spring (4) spring boot (3) Spring Examples (1) Spring Features (1) spring jpa (1) Stack (1) Streams & IO (3) Strings (11) SW Developer Tools (2) testing (1) troubleshooting (1) user interface (1) vxml (8) web services (1) Web Technologies (1) Web Technology Books (1) youtube (1)