从 Java 中的 TreeMap 中移除所有值
使用 clear() 方法从 TreeMap 中移除所有值。
我们首先创建一个 TreeMap −
TreeMap<Integer,String> m = new TreeMap<Integer,String>();
向 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");
现在我们移除所有值 −
m.clear();
以下是从 TreeMap 中移除所有值的示例 −
示例
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()); } }
输出
TreeMap Elements... PHP jQuery JavaScript Ruby Java AngularJS ExpressJS TreeMap size = 7 TreeMap after removing all elements = {} Size of TreeMap now = 0
广告