TreeMap void clear() Method Example Program


Removes all of the mappings from this map. The map will be empty after this call returns.

Program

package com.candidjava;

import java.util.TreeMap;
import java.util.NavigableMap;

/**
 * @author: Hari Haran
 * @discription: The clear() method is used to remove all of the mappings from
 *               this map. The map will be empty after this call returns.
 */

public class TreeMapClear {
	public static void main(String[] args) {

		NavigableMap<Integer, String> tm = new TreeMap<Integer, String>();

		tm.put(1, "Anand");
		tm.put(2, "Hari Haran");
		tm.put(8, "Kamal");
		tm.put(4, "Karthikeyan");
		tm.put(5, "Mohan");
		tm.put(6, "Vinoth");
		tm.put(7, "Hari Haran");

		System.out.println("Entries in the Map: " + tm);

		System.out.println("Clearing the Map");
		tm.clear();

		System.out.println("Entries in the Map: " + tm);
	}
}

Output

Entries in the Map: {1=Anand, 2=Hari Haran, 4=Karthikeyan, 5=Mohan, 6=Vinoth, 7=Hari Haran, 8=Kamal}
Clearing the Map
Entries in the Map: {}

Explanation

public void clear()
Removes all of the mappings from this map. The map will be empty after this call returns.
Specified by:
clear in interface Map<K,V>
Overrides:
clear in class AbstractMap<K,V>


Related Post

Comments


©candidjava.com