Golang Tutorials - Learn Go Programming with Easy Step-by-Step Guides

Explore comprehensive Golang tutorials for beginners and advanced programmers. Learn Go programming with easy-to-follow, step-by-step guides, examples, and practical tips to master Go language quickly.

Java Hashmap Methods in Java

Java Hashmap Methods in Java

HashMap Methods in Java

The HashMap class in Java provides a variety of methods that you can use to manipulate and access key-value pairs. Below is a detailed list of commonly used methods in the HashMap class, all of which are part of the java.util package.


1. put(K key, V value)

  • Description: Adds a key-value pair to the HashMap. If the key already exists, the old value is replaced with the new one.

  • Syntax:

    map.put(key, value);
  • Example:

    map.put(1, "Apple");map.put(2, "Banana");

2. get(Object key)

  • Description: Retrieves the value associated with the specified key. If the key is not found, it returns null.

  • Syntax:

    V value = map.get(key);
  • Example:

    String value = map.get(1);  // Returns "Apple"

3. containsKey(Object key)

  • Description: Checks if the specified key exists in the HashMap.

  • Syntax:

    boolean containsKey = map.containsKey(key);
  • Example:

    boolean exists = map.containsKey(2);  // Returns true

4. containsValue(Object value)

  • Description: Checks if the specified value exists in the HashMap.

  • Syntax:

    boolean containsValue = map.containsValue(value);
  • Example:

    boolean exists = map.containsValue("Banana");  // Returns true

5. remove(Object key)

  • Description: Removes the key-value pair for the specified key. Returns the value that was removed or null if the key was not found.

  • Syntax:

    V removedValue = map.remove(key);
  • Example:

    String removed = map.remove(1);  // Removes and returns "Apple"

6. clear()

  • Description: Removes all key-value pairs from the HashMap.

  • Syntax:

    map.clear();
  • Example:

    map.clear();  // Clears the HashMap

7. size()

  • Description: Returns the number of key-value pairs in the HashMap.

  • Syntax:

    int size = map.size();
  • Example:

    int size = map.size();  // Returns the number of entries in the map

8. isEmpty()

  • Description: Returns true if the HashMap is empty (contains no key-value pairs), otherwise false.

  • Syntax:

    boolean isEmpty = map.isEmpty();
  • Example:

    boolean empty = map.isEmpty();  // Returns false if the map has entries

9. keySet()

  • Description: Returns a Set view of all the keys contained in the HashMap.

  • Syntax:

    Set<K> keys = map.keySet();
  • Example:

    Set<Integer> keys = map.keySet();  // Returns a Set of all keys

10. values()

  • Description: Returns a Collection view of all the values contained in the HashMap.

  • Syntax:

    Collection<V> values = map.values();
  • Example:

    Collection<String> values = map.values();  // Returns a Collection of all values

11. entrySet()

  • Description: Returns a Set view of all key-value pairs in the HashMap (entries).

  • Syntax:

    Set<Map.Entry<K, V>> entries = map.entrySet();
  • Example:

    Set<Map.Entry<Integer, String>> entries = map.entrySet();for (Map.Entry<Integer, String> entry : entries) {    System.out.println(entry.getKey() + ": " + entry.getValue());}

12. putIfAbsent(K key, V value)

  • Description: Adds the key-value pair to the HashMap if the specified key is not already present in the map. If the key already exists, it does nothing.

  • Syntax:

    map.putIfAbsent(key, value);
  • Example:

    map.putIfAbsent(3, "Cherry");  // Adds "Cherry" only if key 3 doesn't exist

13. replace(K key, V value)

  • Description: Replaces the value for the specified key if the key exists.

  • Syntax:

    map.replace(key, value);
  • Example:

    map.replace(2, "Blueberry");  // Replaces "Banana" with "Blueberry" for key 2

14. replace(K key, V oldValue, V newValue)

  • Description: Replaces the value for the specified key if the current value is equal to the oldValue.

  • Syntax:

    boolean replaced = map.replace(key, oldValue, newValue);
  • Example:

    boolean replaced = map.replace(2, "Banana", "Blueberry");  // Replaces if value is "Banana"

15. compute(K key, BiFunction<? super K,? super V,? extends V> remappingFunction)

  • Description: Computes a new value for the given key using the provided remapping function. If the key is not present, it will be created.

  • Syntax:

    map.compute(key, (k, v) -> v == null ? "New Value" : v.toUpperCase());
  • Example:

    map.compute(3, (k, v) -> v == null ? "Default" : v.toUpperCase());  // Computes value for key 3

16. computeIfAbsent(K key, Function<? super K,? extends V> mappingFunction)

  • Description: Computes a value for the specified key only if the key is not already present in the map.

  • Syntax:

    map.computeIfAbsent(key, k -> "New Value");
  • Example:

    map.computeIfAbsent(4, k -> "Dragonfruit");  // Adds "Dragonfruit" if key 4 doesn't exist

17. computeIfPresent(K key, BiFunction<? super K,? super V,? extends V> remappingFunction)

  • Description: Computes a new value for the specified key only if the key is already present in the map.

  • Syntax:

    map.computeIfPresent(key, (k, v) -> v.toLowerCase());
  • Example:

    map.computeIfPresent(2, (k, v) -> v + " Fruit");  // Modifies value for key 2 if present

18. forEach(BiConsumer<? super K,? super V> action)

  • Description: Performs the specified action for each entry in the HashMap.

  • Syntax:

    map.forEach((key, value) -> System.out.println(key + ": " + value));
  • Example:

    map.forEach((key, value) -> System.out.println(key + ": " + value));

Conclusion

The HashMap class provides a variety of methods to manipulate and interact with key-value pairs in a map. The methods are designed to make adding, retrieving, removing, and checking key-value pairs efficient and straightforward. By understanding and using these methods, you can easily manage data in your Java applications.

If you have any questions about specific methods or need more examples, feel free to ask! ?

Disclaimer for AI-Generated Content:
The content provided in these tutorials is generated using artificial intelligence and is intended for educational purposes only.
html
docker
php
kubernetes
golang
mysql
postgresql
mariaDB
sql