HashMap初步使用
来源:互联网 发布:淘宝旅行网火车 编辑:程序博客网 时间:2024/05/22 00:16
HashMap是一种存储映射的集合,它包括key和value,类型可以是任何类型
1.定义:
HashMap<String, Person> hm = new HashMap<String, Person>();Person p1 = new Person("张三", "男", "12345678");Person p2 = new Person("李四", "女", "46464565");Person p3 = new Person("王五", "女", "99524722");Person p4 = new Person("马六", "男", "88520477");hm.put(p1.getName(), p1);hm.put(p2.getName(), p2);hm.put(p3.getName(), p3);hm.put(p4.getName(), p4);
在使用HaspMap时可以利用HashMap对象的get(key)方法通过key来获取对应的value
2.迭代访问:
1)调用entrySet()方法:
Set<Entry<String, Person>> set = hm.entrySet();//类型必须一致Iterator<Entry<String, Person>> itx = set.iterator();Set类可以产生迭代器iteratorwhile (itx.hasNext()) {Entry<String, Person> entry = itx.next();String key = entry.getKey();Person p = entry.getValue();//操作代码}2) 调用keySet()方法:
Set<String> tmp= hm.keySet();Iterator<String> itx2 = tmp.iterator();
- HashMap初步使用
- HashMap使用
- 使用HashMap
- HashMap使用
- HashMap使用
- hashmap 使用
- 练习使用HashMap
- HashMap 使用小结
- HashMap使用的注意事项
- HashMap 使用小结
- HashMap 使用小结
- HashMap 使用小结
- hashmap的使用
- HashMap的使用
- HashMap使用范例
- 使用Iterator遍历hashmap
- 使用entrySet遍历HashMap
- hashmap的使用
- CentOS6.3安装VNC远程桌面【RealVNC5.0】
- EINTR 错误码4
- c++ 构造函数不能是虚函数?
- 树形控件QTreeView添加右键菜单
- 光标自动定位到输入框
- HashMap初步使用
- 大数的阶乘---可求1000!的值,位数,还可求数中2的个数(1个数组 简单易理解)
- C++中的virtual
- android中的线程
- CreateProcess()函数详细解释
- C语言字符串数组的例题
- 中文句子相似度判断源码
- Android 仿QQ主页
- hdu 2555 人人都能参加第30届校田径运动会了