The powere of HashMap in Java

Most developers use maps to store and get data especially HashMaps, but most of theme dont know how to use it or understand how  HashMaps works.  in this post , i’ll explain how the java.util.HashMap works , we will see different examples of HashMap, like adding and removing entries, iterating over Java HashMap, checking size map, finding if a key or value exists on Map and various other examples, which we used frequently.

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.

Below given is the list of constructors supported by the HashMap class.

Constructors:

  • HashMap( ) : This constructor constructs a default HashMap.
  • HashMap(Map m) : This constructor initializes the hash map by using the elements of the given Map object m
  • HashMap(int capacity) :This constructor initializes the capacity of the hash map to the given integer value, capacity.
  • HashMap(int capacity, float fillRatio) : This constructor initializes both the capacity and fill ratio of the hash map by using its arguments. 

Methods:

HashMap defines the following methods:

  • void clear() : Removes all mappings from this map.
  • Object clone() : Returns a shallow copy of this HashMap instance: the keys and values themselves are not cloned.
  • boolean containsKey(Object key) : Returns true if this map contains a mapping for the specified key.
  • boolean containsValue(Object value): Returns true if this map maps one or more keys to the specified value.
  • Set entrySet() : Returns a collection view of the mappings contained in this map.
  • Object get(Object key): Returns the value to which the specified key is mapped in this identity hashap, or null if the map contains no mapping for this key.
  • boolean isEmpty():Returns true if this map contains no key-value mappings.
  • Set keySet():Returns a set view of the keys contained in this map.
  • Object put(Object key, Object value) : Associates the specified value with the specified key in this map.
  • putAll(Map m) : Copies all of the mappings from the specified map to this map These mappings will replace any mappings that this map had for any of the keys currently in the specified map.
  • Object remove(Object key): Removes the mapping for this key from this map if present.
  • int size():Returns the number of key-value mappings in this map.
  • Collection values():Returns a collection view of the values contained in this map.

There are exemples how to use HashMap to create, remove, iterate through HashMap….

How to create HashMap In Java

Capture_1

Here is the oupt put:

Exemple 1 HashMap

Author: Ahmed HLISSA

http://youcommit.blogspot.com/

HashMap = {1=Ahmed, 2=ALI, 3=EMILIE, 4=Bruno, 5=John}.

How to iterate through HashMap

Capture_2  


Output:

Exemple 2 HashMap

Author: Ahmed HLISSA

http://youcommit.blogspot.com/

key =1 value = Ahmed

key =2 value = ALI

key =3 value = EMILIE

key =4 value = Bruno

key =5 value = John

How to storing Java Object In HashMap

Capture_3

In this exemple we try to store a list of java objects “Person” in a map and iterate through this map to list all stored objects and print their caractéristiques.

This is our class Person:

We add a list of persons in the map:

And the result is:


 First name = ALI last name = ALAOUI

 First name = SARA last name = ALAMI

 First name = BRAD last name = PITT

 First name = john last name = travolta
I hope that this post was useful form you. below :

Further references …

Java HashMap exemples

HashMap Class

How HashMap works in Java

10 Examples of HashMap in Java

Good luck !

Advertisements

3 thoughts on “The powere of HashMap in Java

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s