HashMap Tutorial Java – Complete Programming Example


In this chapter you will learn

  • HashMap Tutorial Java
  • HashMap Programming Example
  • Constructores, Methdods and Properties of HashMap
  • What is HashMap Class in Java?

    HashMap is the same as HashTable class and only difference is it allows inserting null values and it is unsynchronized. HashMap doesn’t maintain insertion order and the elements can be stored at any places and iteration result might be different than the insertion order. It is also based on Map Interface and inherits all the functionality of Map Interface. More about HashMap class in java are listed below in the list.

    1. It contains value based on Key.
    2. It allows inserting null values and null keys.
    3. There may be only one null key in the list but can have multiple null values.
    4. It doesn’t maintain insertion order.
    5. It uses HashTable to implement the Map Interface.

    Constructors and Methods

    ConstructorDescription
    HashMap()It is used to construct a default HashMap.
    HashMap(Map m)It is used to initializes the hash map by using the elements of the given Map object m.
    HashMap(int capacity)It is used to initializes the capacity of the hash map to the given integer value, capacity.
    HashMap(int capacity, float fillRatio)It is used to initialize both the capacity and fill ratio of the hash map by using its arguments

    Methods

    MethodsDescription
    void clear()It is used to remove all of the mappings from this map.
    boolean containsKey(Object key)It is used to return true if this map contains a mapping for the specified key.
    boolean containsValue(Object value)It is used to return true if this map maps one or more keys to the specified value.
    boolean isEmpty()It is used to return true if this map contains no key-value mappings.
    Object clone()It is used to return a shallow copy of this HashMap instance: the keys and values themselves are not cloned.
    Set entrySet()It is used to return a collection view of the mappings contained in this map.
    Set keySet()It is used to return a set view of the keys contained in this map.
    Object put(Object key, Object value)It is used to associate the specified value with the specified key in this map.
    int size()It is used to return the number of key-value mappings in this map.
    Collection values()It is used to return a collection view of the values contained in this map.

    Programming Example

    Output

    {null=null, 1=Steven, 3=Jack, 6=Clark}
    Key is : null and Value is : null
    Key is : 1 and Value is : Steven
    Key is : 3 and Value is : Jack
    Key is : 6 and Value is : Clark
    Key : null | Value = null
    Key : 1 | Value = Steven
    Key : 3 | Value = Jack
    Key : 6 | Value = Clark
    Item Removed : Steven
    _

    Summary

    The HashMap class is another implementaion of Map Interface that uses HashTable. In this chapter you learn all about HashMap class in Java with complete programming example. In the next chapter you will learn LinkedHashMap class in Java.


    Leave a Reply

    Your email address will not be published. Required fields are marked *