源码:

 package test_demo;

 import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Random; /*
* @desc HashMap测试程序
*/ public class HashMapDemo {
private static void testHashMapAPIs() {
// 初始化随机种子
Random r = new Random();
// 新建HashMap
HashMap map = new HashMap();
// 添加操作
map.put("one", r.nextInt(10));
map.put("two", r.nextInt(10));
map.put("three", r.nextInt(10));
// 打印出map
System.out.println("map:" + map);
// 通过Iterator遍历key-value
Iterator iter = map.entrySet().iterator();
while (iter.hasNext()) {
Map.Entry entry = (Map.Entry) iter.next();
System.out.println("next : " + entry.getKey() + ":" + entry.getValue());
}
// HashMap的键值对个数
System.out.println("size:" + map.size());
// containsKey(Object key) :是否包含键key
System.out.println("contains key two : " + map.containsKey("two"));
System.out.println("contains key five : " + map.containsKey("five"));
// containsValue(Object value) :是否包含值value
System.out.println("contains value 0 : " + map.containsValue(new Integer(0)));
// remove(Object key) : 删除键key对应的键值对
map.remove("three");
System.out.println("删除three");
System.out.println("map:" + map);
// clear() : 清空HashMap
map.clear();
System.out.println("清空HashMap");
// isEmpty() : HashMap是否为空
System.out.println((map.isEmpty() ? "map is empty" : "map is not empty"));
} public static void main(String[] args) {
testHashMapAPIs();
}
}

执行结果:

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

  

最新文章

  1. Lind.DDD.UoW~方法回调完成原子化操作
  2. 判断iframe是否加载完成的完美方法
  3. ThinkPad W520 在 Windows Server 2012 / R2 中安装驱动
  4. 通过生产者消费者模式例子讲解Java基类方法wait、notify、notifyAll
  5. Unity 坐标系
  6. php多线程pthreads的安装与使用
  7. centos6.7 本地yum源配置
  8. Android系统中设置TextView等的行间距
  9. IIS7.5 HTTP 错误 500.19 - Internal Server Error 问题的解决方案
  10. linux 安装软件的地方
  11. Windows中的对象
  12. Python之路day4
  13. [认证授权] 4.OIDC(OpenId Connect)身份认证授权(核心部分)
  14. 初试pyspider
  15. linux下的外网木马前期要的工具
  16. 阶乘之和 输入n,计算S=1!+2!+3!+…+n!的末6位(不含前导0)。n≤10 6 ,n!表示 前n个正整数之积。
  17. python note 13 内置函数
  18. Python操作Mysql数据库进阶篇——查询操作详解(一)
  19. 如何运行ruby代码
  20. delphi reintroduce作用

热门文章

  1. OpenCV——图像修补
  2. Python2.7-operator
  3. 关于OpenCV2.4.9在VS2012上的配置
  4. Hadoop体系结构杂谈
  5. 【LeetCode234】Palindrome Linked List★
  6. go语言之行--基础部分
  7. 20155302《网络对抗》Exp7 网络欺诈防范
  8. 20155320 Exp3 免杀原理与实践
  9. 20155333 《网络对抗》 Exp5 MSF基础应用
  10. Java实现Zip压缩包解压