标签:执行 package system 源码 随机种子 imp size asn highlight
源码:
1 package test_demo; 2 3 import java.util.HashMap; 4 import java.util.Iterator; 5 import java.util.Map; 6 import java.util.Random; 7 8 /* 9 * @desc HashMap测试程序 10 */ 11 12 public class HashMapDemo { 13 private static void testHashMapAPIs() { 14 // 初始化随机种子 15 Random r = new Random(); 16 // 新建HashMap 17 HashMap map = new HashMap(); 18 // 添加操作 19 map.put("one", r.nextInt(10)); 20 map.put("two", r.nextInt(10)); 21 map.put("three", r.nextInt(10)); 22 // 打印出map 23 System.out.println("map:" + map); 24 // 通过Iterator遍历key-value 25 Iterator iter = map.entrySet().iterator(); 26 while (iter.hasNext()) { 27 Map.Entry entry = (Map.Entry) iter.next(); 28 System.out.println("next : " + entry.getKey() + ":" + entry.getValue()); 29 } 30 // HashMap的键值对个数 31 System.out.println("size:" + map.size()); 32 // containsKey(Object key) :是否包含键key 33 System.out.println("contains key two : " + map.containsKey("two")); 34 System.out.println("contains key five : " + map.containsKey("five")); 35 // containsValue(Object value) :是否包含值value 36 System.out.println("contains value 0 : " + map.containsValue(new Integer(0))); 37 // remove(Object key) : 删除键key对应的键值对 38 map.remove("three"); 39 System.out.println("删除three"); 40 System.out.println("map:" + map); 41 // clear() : 清空HashMap 42 map.clear(); 43 System.out.println("清空HashMap"); 44 // isEmpty() : HashMap是否为空 45 System.out.println((map.isEmpty() ? "map is empty" : "map is not empty")); 46 } 47 48 public static void main(String[] args) { 49 testHashMapAPIs(); 50 } 51 }
执行结果:
map:{one=1, two=9, three=2} next : one:1 next : two:9 next : three:2 size:3 contains key two : true contains key five : false contains value 0 : false 删除three map:{one=1, two=9} 清空HashMap map is empty
标签:执行 package system 源码 随机种子 imp size asn highlight
原文地址:http://www.cnblogs.com/gongxr/p/7813772.html