In this post more methods are discussed.
keySet(): java.util.HashMap.keySet() It returns a Set view of the keys contained in this map. The set is backed by the map, so changes to the map are reflected in the set, and vice-versa.
Syntax: public Set keySet() Return: a set view of the keys contained in this map
values(): java.util.HashMap.values() It returns a Collection view of the values contained in this map. The collection is backed by the map, so changes to the map are reflected in the collection, and vice-versa.
Syntax: public Collection values() Return: a collection view of the values contained in this map
containsKey(): java.util.HashMap.containsKey() It returns true if this map maps one or more keys to the specified value.
Syntax: public boolean containsValue(Object value) Parameters: value - value whose presence in this map is to be tested Return: true if this map maps one or more keys to the specified value
Testing .containsKey : HashMap Initial keys : [Language, Platform, Learn, Code] Initial values : [Java, Geeks For geeks, More, HashMap] New Keys : [Language, Platform, Search, Learn, Code] New Values: [Java, Geeks For geeks, JavaArticle, More, HashMap]
Syntax: public Set<Map.Entry> entrySet() Return: complete set of keys and values
It is very nice way to assign values to the keys that are not yet mapped, without interfering with the already present set of keys and values.
Syntax: default V getOrDefault(Object key,V defaultValue) Parameters: key - the key whose mapped value we need to return defaultValue - the default for the keys present in HashMap Return: mapping the unmapped keys with the default value.
1st method accepts set of key and value which will replace the already present value of the key with the the new value passed in the argument. If no such set is present replace() method will do nothing.
Meanwhile 2nd method will only replace the already present set of key-old_value if the key and old_Value are found in the HashMap.
Syntax: replace(k key, v value) or replace(k key, v oldvalue, newvalue) Parameters: key - key in set with the old value. value - new value we want to be with the specified key oldvalue - old value in set with the specified key newvalue - new value we want to be with the specified key Return: True - if the value is replaced Null - if there is no such set present
Syntax: public V putIfAbsent(key, value) Parameters: key - key with which the specified value is associates. value - value to associates with the specified key.
Set of Keys and Values using entrySet() : [Language=Java, Learn=More, Code=HashMap] Using .getorDefault : HashMap Using .getorDefault : javaArticle working of .replace() : [Language=Java, Learn=Methods, Code=HashMap] working of .putIfAbsent() : [Language=Java, cool=HashMap methods, Learn=Methods, Code=HashMap] working of .putIfAbsent() : [Language=Java, cool=HashMap methods, Learn=Methods, Code=HashMap]
Set of Keys and Values : [Language=Java, Platform=Geeks For geeks, Code=HashMap] Use of Iterator to remove the sets. Set of Keys and Values : [Platform=Geeks For geeks, Code=HashMap] Set of Keys and Values : [Code=HashMap] Set of Keys and Values : 
If we use for loop, it get translated to Iterator internally but without using Iterator explicitly we can’t remove any entry during Iteration.On doing so, Iterator may throw ConcurrentModificationException. So, we use explicit Iterator and while loop to traverse.
This article is contributed by Mohit Gupta. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
- How to iterate any Map in Java
- Internal Working of HashMap in Java
- HashMap in Java
- Vector vs ArrayList in Java
- StringTokenizer class in Java with example | Set 1 ( Constructors)
- HashMap Class Methods in Java with Examples | Set 1 (put(), get(), isEmpty() and size())
- Collections.sort() in Java with Examples
- Map Interface in Java
- SortedSet Interface in Java with Examples
- LinkedHashMap in Java
- HashSet in Java
- Collections in Java
- HashMap and TreeMap in Java
- Set in Java
- Differences between HashMap and HashTable in Java