- 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 value from TreeMap in Java
Use the remove() method to remove a value from TreeMap.
Create a TreeMap first and add some elements −
TreeMap<Integer,String> m = new TreeMap<Integer,String>(); m.put(1,"PHP"); m.put(2,"jQuery"); m.put(3,"JavaScript"); m.put(4,"Ruby"); m.put(5,"Java"); m.put(6,"AngularJS"); m.put(7,"ExpressJS");
Now, let’s say you need to remove a value with key 5. For that, use the remove() method −
Object ob = m.remove(5);
The following is an example to remove a value 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,"PHP"); m.put(2,"jQuery"); m.put(3,"JavaScript"); m.put(4,"Ruby"); m.put(5,"Java"); m.put(6,"AngularJS"); m.put(7,"ExpressJS"); System.out.println("TreeMap Elements..."); Collection res = m.values(); Iterator i = res.iterator(); while (i.hasNext()) { System.out.println(i.next()); } System.out.println("TreeMap size = "+m.size()); // Remove an element Object ob = m.remove(5); System.out.println("
TreeMap after removing a single element...
"+m); System.out.println("Size of TreeMap now = "+m.size()); } }
Output
TreeMap Elements... PHP jQuery JavaScript Ruby Java AngularJS ExpressJS TreeMap size = 7 TreeMap after removing a single element... {1=PHP, 2=jQuery, 3=JavaScript, 4=Ruby, 6=AngularJS, 7=ExpressJS} Size of TreeMap now = 6
- Related Articles
- Remove a key 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
- Remove a value from Java LinkedHashMap
- Remove the first entry of the TreeMap in Java
- Remove the last entry of the TreeMap in Java
- Remove value from HashMap in Java
- Create NavigableMap from TreeMap in Java
- remove null value from a String array in Java
- Create a TreeMap in Java and add key-value pairs
- Get Sub Map from TreeMap in Java
- Get Tail Map from TreeMap in Java
- Get Head Map from TreeMap in Java
- Java Program to check if a particular value exists in TreeMap
- How to remove data from treemap to arraylist for listview in Android?

Advertisements