Skip to content
geeksforgeeks
  • Tutorials
    • Python
    • Java
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
    • Practice Coding Problems
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Java Arrays
  • Java Strings
  • Java OOPs
  • Java Collection
  • Java 8 Tutorial
  • Java Multithreading
  • Java Exception Handling
  • Java Programs
  • Java Project
  • Java Collections Interview
  • Java Interview Questions
  • Java MCQs
  • Spring
  • Spring MVC
  • Spring Boot
  • Hibernate
Open In App
Next Article:
TreeMap put() Method in Java
Next article icon

TreeMap put() Method in Java

Last Updated : 10 Jul, 2018
Comments
Improve
Suggest changes
Like Article
Like
Report
The java.util.TreeMap.put() method of TreeMap 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:
Tree_Map.put(key, value)
Parameters: The method takes two parameters, both are of the Object type of the TreeMap.
  • 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 java.util.TreeMap.put() Method: Program 1: When passing an existing key. Java
// Java code to illustrate the put() method import java.util.*;  public class Tree_Map_Demo {     public static void main(String[] args)     {          // Creating an empty TreeMap         TreeMap<Integer, String> tree_map = new TreeMap<Integer, String>();          // Mapping string values to int keys         tree_map.put(10, "Geeks");         tree_map.put(15, "4");         tree_map.put(20, "Geeks");         tree_map.put(25, "Welcomes");         tree_map.put(30, "You");          // Displaying the TreeMap         System.out.println("Initial Mappings are: " + tree_map);          // Inserting existing key along with new value         String returned_value = (String)tree_map.put(20, "All");          // Verifying the returned value         System.out.println("Returned value is: " + returned_value);          // Displayin the new map         System.out.println("New map is: " + tree_map);     } } 
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. Java
// Java code to illustrate the put() method import java.util.*;  public class Tree_Map_Demo {     public static void main(String[] args)     {          // Creating an empty TreeMap         TreeMap<Integer, String> tree_map = new TreeMap<Integer, String>();          // Mapping string values to int keys         tree_map.put(10, "Geeks");         tree_map.put(15, "4");         tree_map.put(20, "Geeks");         tree_map.put(25, "Welcomes");         tree_map.put(30, "You");          // Displaying the TreeMap         System.out.println("Initial Mappings are: " + tree_map);          // Inserting existing key along with new value         String returned_value = (String)tree_map.put(50, "All");          // Verifying the returned value         System.out.println("Returned value is: " + returned_value);          // Displayin the new map         System.out.println("New map is: " + tree_map);     } } 
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.

Next Article
TreeMap put() Method in Java

C

chinmoy lenka
Improve
Article Tags :
  • Misc
  • Java
  • Java-Collections
  • Java-Functions
  • java-TreeMap
Practice Tags :
  • Java
  • Java-Collections
  • Misc

Similar Reads

    TreeMap in Java
    TreeMap is a part of the Java Collection Framework. It implements the Map and NavigableMap interface and extends the AbstractMap class. It stores key-value pairs in a sorted order based on the natural ordering of keys or a custom Comparator. It uses a Red-Black Tree for efficient operations (add, re
    11 min read
    Java TreeMap clear() Method
    The clear() method is a built-in method of the TreeMap class in Java of the java.util package. This method is used to remove all key-value mappings from the TreeMap. And after invoking this method, the map becomes completely empty.This method is very useful when we want to reset the map or discard a
    2 min read
    TreeMap clone() Method in Java with Examples
    In Java, clone() method of the TreeMap class is used to return a shallow copy of the mentioned treemap. It just creates a copy of the map. --> java.util Package --> TreeMap Class --> clone() Method Syntax: Tree_Map.clone() Parameters: The method does not take any parameters. Return Type: A
    2 min read
    Java TreeMap containsKey() Method
    The containsKey() method in Java is used to check if a given key exists in a TreeMap. If the key is present in the map, it returns true, otherwise, it returns false. This method is very helpful when we need to verify whether a mapping exists before performing any operation on it.Syntax of TreeMap co
    3 min read
    TreeMap containsValue() Method in Java With Examples
    In Java, containsValue() method of the TreeMap class is used to check whether a particular value is being mapped by any key in the TreeMap. It takes the value as a parameter and returns True if that value is mapped by any of the keys in the map. --> java.util Package --> TreeMap class --> c
    3 min read
    TreeMap entrySet() Method in Java
    The java.util.TreeMap.entrySet() method in Java is used to create a set out of the same elements contained in the treemap. It basically returns a set view of the treemap or we can create a new set and store the map elements into them. Syntax: tree_map.entrySet() Parameters: The method does not take
    2 min read
    TreeMap firstEntry() and firstKey() Method in Java with Examples
    There are two variants of first() in Java.util.TreeMap, both are discussed in this article. Method 1: firstEntry() It returns a key-value mapping associated with the least key in this map, or null if the map is empty. Syntax: public Map.Entry firstEntry() Return Type: An entry with the least key and
    3 min read
    Java TreeMap get() Method
    The get() method of the TreeMap class in Java is used to retrieve or fetch the value mapped by a particular key mentioned in the parameter. If the key does not exist in the map, the method returns null.Syntax of TreeMap get() MethodtreeMap.get(Object key)Parameter: key: The method takes one paramete
    2 min read
    TreeMap headMap() Method in Java
    The java.util.TreeMap.headMap(key_point) method of TreeMap class is used to get all the pairs or portion of the map strictly less than the parameter key_value. The mentioned parameter is excluded from the newly prepared treemap. Since the set is backed by the map, so any changes to the map are refle
    3 min read
    TreeMap keySet() Method in Java with Examples
    In Java, keySet() method of TreeMap class is present inside java.util package in Java is used to create a set out of the key elements contained in the treemap. It basically returns a set view of the keys or we can create a new set and store the key elements in them in ascending order. Since the set
    3 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences