DEV Community

loizenai
loizenai

Posted on

How to work with Java HashMap

https://grokonez.com/java/java-advanced/work-java-hashmap

How to work with Java HashMap

In the tutorial, JavaSampleApproach will introduce you how to work with Java HashMap

I. Concepts

Java HashMap is a construction of Map interface, so it provides all methods of Map interface.
Related post: How to work with Java Map Interface

  • It permits a null key and null values HashMap provides 3 Constructors:
  • HashMap(): default initial capacity (16) and the default load factor (0.75).
  • HashMap(int initialCapacity, float loadFactor)
  • HashMap(Map<? extends K,? extends V> m): constructs a new HashMap with the same mappings as the specified Map. Note: In common case, we use empty constructor HashMap() & HashMap(Map<? extends K,? extends V> m) constructors. Example:
    
    //a void (no arguments) constructor
    Map map = new HashMap();

//a constructor with a single argument of type Map
Map newMap = new HashMap(map);

For insert and retrieve a key-value mapping, we uses: put() & get() methods.

  • put(K key, V value)
  • putAll(Map<? extends K,? extends V> m)
  • get(Object key)

More at:

https://grokonez.com/java/java-advanced/work-java-hashmap

How to work with Java HashMap

Top comments (0)