VOOZH about

URL: https://www.geeksforgeeks.org/java/navigablemap-put-method-in-java/

⇱ NavigableMap put() Method in Java - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

NavigableMap put() Method in Java

Last Updated : 31 Oct, 2018
The put() method of NavigableMap Interface is used to insert a mapping into a map. This means we can insert a specific key and the value it is mapping to into a particular map. If an existing key is passed then the previous value gets replaced by the new value. If a new pair is passed, then the pair gets inserted as a whole. Syntax:
NavigableMap.put(key, value)
Parameters: The method takes two parameters, both are of the Object type of the Map taken.
  • key: This refers to the key element that needs to be inserted into the Map for mapping.
  • value: This refers to the value that the above key would map into.
Return Value: If an existing key is passed then the previous value gets returned. If a new pair is passed, then NULL is returned. Below programs are used to illustrate the working of put() method: Program 1: When passing an existing key.
Output:
Initial Mappings are: {10=Geeks, 15=4, 20=Geeks, 25=Welcomes, 30=You}
Returned value is: Geeks
New map is: {10=Geeks, 15=4, 20=All, 25=Welcomes, 30=You}
Program 2: When passing a new key.
Output:
Initial Mappings are: {10=Geeks, 15=4, 20=Geeks, 25=Welcomes, 30=You}
Returned value is: null
New map is: {10=Geeks, 15=4, 20=Geeks, 25=Welcomes, 30=You, 50=All}
Note: The same operation can be performed with any type of Mappings with variation and combination of different data types.
Comment
Article Tags: