SortedMap clear() method in Java with Examples Last Updated : 30 Sep, 2019 Summarize Comments Improve Suggest changes Share Like Article Like Report The clear() method in SortedMap Java is used to clear and remove all of the elements or mappings from a specified SortedMap collection. Syntax: void clear() Parameters: The method does not accept any parameters. Return Value: The method does not return any value. Note: The clear() method in SortedMap is inherited from the Map interface in Java. Below programs are used to illustrate the working of clear() Method: Program 1: Mapping String Values to Integer Keys. Java // Java code to illustrate the clear() method import java.util.*; public class Map_Demo { public static void main(String[] args) { // Creating an empty SortedMap SortedMap<Integer, String> map = new TreeMap<Integer, String>(); // Mapping string values to int keys map.put(10, "Geeks"); map.put(15, "4"); map.put(20, "Geeks"); map.put(25, "Welcomes"); map.put(30, "You"); // Displaying the Map System.out.println( "Initial Mappings are: " + map); // Clearing the sorted map using clear() map.clear(); // Displaying the final SortedMap System.out.println( "Finally the maps" + " look like this: " + map); } } Output: Initial Mappings are: {10=Geeks, 15=4, 20=Geeks, 25=Welcomes, 30=You} Finally the maps look like this: {} Program 2: Mapping Integer Values to String Keys. Java // Java code to illustrate the clear() method import java.util.*; public class Map_Demo { public static void main(String[] args) { // Creating an empty SortedMap SortedMap<String, Integer> map = new TreeMap<String, Integer>(); // Mapping int values to string keys map.put("Geeks", 10); map.put("4", 15); map.put("Geeks", 20); map.put("Welcomes", 25); map.put("You", 30); // Displaying the SortedMap System.out.println( "Initial Mappings are: " + map); // Clearing the SortedMap using clear() map.clear(); // Displaying the final Map System.out.println( "Finally the maps " + "look like this: " + map); } } Output: Initial Mappings are: {4=15, Geeks=20, Welcomes=25, You=30} Finally the maps look like this: {} Note: The same operation can be performed with any type of Mapping with variation and combination of different data types. Reference: https://docs.oracle.com/javase/7/docs/api/java/util/Map.html#clear() Comment More infoAdvertise with us Next Article SortedSet clear() method in Java with Examples G gopaldave Follow Improve Article Tags : Java Java - util package Java-Functions Java-SortedMap Practice Tags : Java Similar Reads SortedSet clear() method in Java with Examples The clear() method is used to remove all the elements from a SortedSet. Using the clear() method only clears all the element from the set and not deletes the set. In other words, we can say that the clear() method is used to only empty an existing Set. Syntax: void clear() Parameters: The method doe 1 min read Set clear() method in Java with Examples The Java.util.Set.clear() method is used to remove all the elements from a Set. Using the clear() method only clears all the element from the set and not deletes the set. In other words, we can say that the clear() method is used to only empty an existing Set. Syntax: void clear() Parameters: The me 1 min read SortedMap comparator() method in Java with Examples The comparator() method of java.util.SortedMap interface is used to return the comparator used to order the keys in this map, or null if this map uses the natural ordering of its keys.Syntax: public Comparator comparator() Return Value: This method returns the comparator used to order the keys in th 2 min read Stack clear() method in Java with Example The Java.util.Stack.clear() method is used to remove all the elements from a Stack. Using the clear() method only clears all the element from the Stack and does not delete the Stack. In other words, we can say that the clear() method is used to only empty an existing Stack. Syntax: Stack.clear() Par 2 min read Properties clear() method in Java with Examples The Java.util.Properties.clear() method is used to remove all the elements from this Properties instance. Using the clear() method only clears all the element from the Properties and does not delete the Properties. In other words, we can say that the clear() method is used to only empty an existing 2 min read SortedMap put() method in Java with Examples The put() method of SortedMap interface in Java is used to associate the specified value with the specified key in this map. Syntax: V put(K key, V value) Parameters: This method has two arguments: key: which is the left argument and value: which is the corresponding value of the key in the map. Ret 2 min read Like