- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
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 all values from TreeMap in Java
Use the clear() method to remove all values from TreeMap.
Let us first create a TreeMap −
TreeMap<Integer,String> m = new TreeMap<Integer,String>();
Add some elements to the TreeMap −
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");
Let us now remove all the values −
m.clear();
The following is an example to remove all values 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()); m.clear(); System.out.println("
TreeMap after removing all elements = "+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 all elements = {} Size of TreeMap now = 0
- Related Articles
- Remove a value from TreeMap in Java
- Remove a key from TreeMap in Java
- Remove all values from HashMap in Java
- Remove all values from Java LinkedHashMap
- Remove the last entry of the TreeMap in Java
- Remove the first entry of the TreeMap in Java
- Iterate through the values of TreeMap in Java
- Create NavigableMap from TreeMap in Java
- Remove all elements from TreeSet in Java
- Remove all elements from Java NavigableMap
- Remove all elements from Java LinkedHashSet
- Get Sub Map from TreeMap in Java
- Get Tail Map from TreeMap in Java
- Get Head Map from TreeMap in Java
- Remove all elements from a HashSet in Java

Advertisements