clear() method

clear(): This clear javascript method is used to remove all the elements from the TreeMap in Java. Its return type is void. It means this method doesn’t returns anything.

void clear();
import java.util.TreeMap;

public class ExampleOfTreeMap 
	public static void main(String[] args) 
		TreeMap<Integer, String> listOfNames = new TreeMap<Integer, String>();
		listOfNames.put(1, "JAVA");
		listOfNames.put(2, "GOAL");
		listOfNames.put(3, "RAVI");
		listOfNames.put(4, "SITE");
		// Before clear() method
		for(String name : listOfNames.values())
			System.out.println("Name from list = "+name);
		// Removing all the element by clear() method
		System.out.println("After Clear() method list is empty : "+ listOfNames.isEmpty());

Output: Name from list = JAVA
Name from list = GOAL
Name from list = RAVI
Name from list = SITE
After Clear() method list is empty : true

Leave a Comment

Follow us on Instagram & watch the latest videos on YouTube. Click below social icons to visit our Instagram & YouTube profiles.