0% found this document useful (0 votes)
5 views2 pages

Java HashMap Methods Overview

The document outlines various methods for manipulating key-value pairs in a map, including adding, retrieving, and removing entries. It provides examples for each method, such as 'put', 'get', 'remove', and 'replace'. Additionally, it describes functions for checking the existence of keys and values, as well as methods for iterating and modifying the map's contents.

Uploaded by

Chirag Saraf
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views2 pages

Java HashMap Methods Overview

The document outlines various methods for manipulating key-value pairs in a map, including adding, retrieving, and removing entries. It provides examples for each method, such as 'put', 'get', 'remove', and 'replace'. Additionally, it describes functions for checking the existence of keys and values, as well as methods for iterating and modifying the map's contents.

Uploaded by

Chirag Saraf
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Method Description Example

put(K key, V value) Associates the specified value with the key [Link]("Chirag", 95);

putIfAbsent(K key, V value) Adds key-value if key not present [Link]("Amit", 88);

get(Object key) Returns value for the key [Link]("Chirag");

remove(Object key) Removes mapping for the key [Link]("Amit");

remove(Object key, Object value) Removes only if key maps to value [Link]("Chirag", 95);

replace(K key, V value) Replaces value for the key [Link]("Chirag", 100);

replace(K key, V oldValue, V newValue) Replaces if oldValue matches [Link]("Chirag", 95, 100);

containsKey(Object key) Checks if key exists [Link]("Chirag");

containsValue(Object value) Checks if value exists [Link](95);

size() Returns number of key-value pairs [Link]();

isEmpty() Checks if map is empty [Link]();

clear() Removes all key-value pairs [Link]();

keySet() Returns a Set of keys [Link]();

values() Returns a Collection of values [Link]();

entrySet() Returns a Set of [Link]<K,V> [Link]();

forEach(BiConsumer action) Performs action for each key-value pair [Link]((k,v)->[Link](k+"->"+v));

compute (K key, BiFunction remappingFunction) Updates value for key [Link]("Chirag", (k,v)->v+5);

computeIfAbsent(K key, Function mappingFunction) Sets value if key absent [Link]("Amit", k->0);

computeIfPresent(K key, BiFunction remappingFunction) Updates value if key present [Link]("Chirag", (k,v)->v+1);

merge (K key, V value, BiFunction remappingFunction) Merges value if key exists [Link]("Chirag", 10, Integer::sum);

getOrDefault(Object key, V defaultValue) Returns value or default [Link]("Ritu", 0);

replaceAll(BiFunction function) Updates all key-value pairs [Link]((k,v)->v+1);

clone() Shallow copy of the map HashMap<String,Integer> copy =


(HashMap<String,Integer>)[Link]

equals(Object o) Checks equality with another Map [Link](map2)

You might also like