- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Remove a key from TreeMap in Java
Use the remove() method to remove a key from TreeMap.
Let us first create a TreeMap and add some elements −
TreeMap<Integer,String> m = new TreeMap<Integer,String>(); m.put(1,"India"); m.put(2,"US"); m.put(3,"Australia"); m.put(4,"Netherlands"); m.put(5,"Canada");
Let us remove a key now. Here, we are removing key 3 now −
m.remove(3)
The following is an example to remove a key from TreeMap −
Example
import java.util.*; public class Demo { public static void main(String args[]) { TreeMap<Integer,String> m = new TreeMap<Integer,String>(); m.put(1,"India"); m.put(2,"US"); m.put(3,"Australia"); m.put(4,"Netherlands"); m.put(5,"Canada"); System.out.println("TreeMap Elements = "+m); System.out.println("Removing a Key = "+m.remove(3)); System.out.println("Updated TreeMap Elements = "+m); } }
Output
TreeMap Elements = {1=India, 2=US, 3=Australia, 4=Netherlands, 5=Canada} Removing a Key = Australia Updated TreeMap Elements = {1=India, 2=US, 4=Netherlands, 5=Canada}
- Related Articles
- Remove a value from TreeMap in Java
- Remove all values from TreeMap in Java
- Java Program to remove a key from a TreeMap only if it is associated with a given value
- Get lowest key stored in Java TreeMap
- Create a TreeMap in Java and add key-value pairs
- Java Program to remove a key from a HashMap
- Java Program to get highest key stored in TreeMap
- Java Program to check if a particular key exists in TreeMap
- Remove the last entry of the TreeMap in Java
- Remove the first entry of the TreeMap in Java
- Create NavigableMap from TreeMap in Java
- Java Program to remove key value pair from HashMap?
- Get Sub Map from TreeMap in Java
- Get Tail Map from TreeMap in Java
- Get Head Map from TreeMap in Java

Advertisements