Java HashMap

https:/‮figi.www/‬tidea.com

In Java, HashMap is a class that implements the Map interface and provides a key-value based data structure. It is part of the java.util package and allows null values and null keys. It does not guarantee the order of the keys and values.

Here are some of the most commonly used methods in the HashMap class:

  1. void clear(): This method removes all the key-value mappings from the HashMap.

  2. boolean containsKey(Object key): This method returns true if the HashMap contains a mapping for the specified key, otherwise false.

  3. boolean containsValue(Object value): This method returns true if the HashMap contains one or more mappings to the specified value, otherwise false.

  4. Set<Map.Entry<K, V>> entrySet(): This method returns a Set view of the key-value mappings in the HashMap.

  5. V get(Object key): This method returns the value to which the specified key is mapped, or null if the key is not mapped to any value.

  6. boolean isEmpty(): This method returns true if the HashMap contains no key-value mappings, otherwise false.

  7. Set<K> keySet(): This method returns a Set view of the keys in the HashMap.

  8. V put(K key, V value): This method associates the specified value with the specified key in the HashMap. If the HashMap previously contained a mapping for the key, the old value is replaced with the new value and the old value is returned.

  9. void putAll(Map<? extends K, ? extends V> m): This method copies all of the mappings from the specified Map to this HashMap.

  10. V remove(Object key): This method removes the mapping for the specified key from the HashMap, if it is present.

  11. int size(): This method returns the number of key-value mappings in the HashMap.

  12. Collection<V> values(): This method returns a Collection view of the values in the HashMap.

These methods provide the basic functionality to manipulate a HashMap. The choice of which method to use depends on the specific requirements of the application.