HashMap基本使用方法
2016-03-28 19:23
351 查看
Collection:list,set,queue
map:键值对存储数据,key,value,key必须唯一
hashMap:无序
treeMap:有序
demo:
public static void main(String[] args) {
// TODO Auto-generated method stub
HashMap<Integer, String> hm = new HashMap<Integer, String>();
hm.put(101, "张三");
hm.put(102, "李四");
hm.put(103, "王五");
hm.put(106, "赵六");
Collection<String> c = hm.values();// 转化为hashSet类型
Iterator<String> ia = c.iterator();
System.out.print("姓名列表:");
while (ia.hasNext()) {
System.out.print(" " + ia.next());
}
System.out.print("\n学好列表");
Collection<Integer> c2 = hm.keySet();
Iterator<Integer> ia2 = c2.iterator();
while (ia2.hasNext()) {
System.out.print(" " + ia2.next());
}
//通过学号,查找值
for(Integer key:c2){
System.out.println("学号:"+key+" -->值:"+hm.get(key));
}
}
输出值:
姓名列表: 张三 李四 王五 赵六
学好列表: 101 102 103 106
学号:101 -->值:张三
学号:102 -->值:李四
学号:103 -->值:王五
学号:106 -->值:赵六
map:键值对存储数据,key,value,key必须唯一
hashMap:无序
treeMap:有序
demo:
public static void main(String[] args) {
// TODO Auto-generated method stub
HashMap<Integer, String> hm = new HashMap<Integer, String>();
hm.put(101, "张三");
hm.put(102, "李四");
hm.put(103, "王五");
hm.put(106, "赵六");
Collection<String> c = hm.values();// 转化为hashSet类型
Iterator<String> ia = c.iterator();
System.out.print("姓名列表:");
while (ia.hasNext()) {
System.out.print(" " + ia.next());
}
System.out.print("\n学好列表");
Collection<Integer> c2 = hm.keySet();
Iterator<Integer> ia2 = c2.iterator();
while (ia2.hasNext()) {
System.out.print(" " + ia2.next());
}
//通过学号,查找值
for(Integer key:c2){
System.out.println("学号:"+key+" -->值:"+hm.get(key));
}
}
输出值:
姓名列表: 张三 李四 王五 赵六
学好列表: 101 102 103 106
学号:101 -->值:张三
学号:102 -->值:李四
学号:103 -->值:王五
学号:106 -->值:赵六
相关文章推荐
- Apktool v2.1.0 Released
- 【JQuery】:input表单选择器
- 【NOIP2012提高组】疫情控制
- JS 事件基础 事件绑定函数
- EaselJS的Text中文不会自动换行的问题
- [SIMD]单指令多数据指令集(一)——SIMD简介
- 第四周项目3-随机数函数应用于游戏(2)
- 机器学习之——认识机器学习
- OpenGL纹理映射--基础篇
- 继承的学习笔记
- Linux下压缩某个文件夹(文件夹打包)
- 第5周-项目2-游戏中的角色类(1)
- [XML]学习笔记(二)——DTD的声明与实体
- 欧几里德算法证明 及代码
- Cacti监控MySQL实现过程中碰到的问题解汇总
- PHP - IteratorAggregate接口详解
- LINUX下查看一个文件夹下所有文件的总大小
- Sublime Text3 无法使用LiveReload插件的解决方法
- 程序分析
- 字符这个东西