HashMap用法
2016-01-15 21:27
363 查看
map键值对,值一般存储的是对象。hashmap中常用的方法,put(object key,object value);
get(object key);//根据key值找出对应的value值。
判断键是否存在:containsKey(object key)
判断值是否存在:containsValue(object value)
1.Map的特性即「键-值」(Key-Value)匹配
java.util.HashMap实作了Map界面,
HashMap在内部实作使用哈希(Hash),很快的时间内可以寻得「键-值」匹配.
2. Map<String, String> map =
new HashMap<String, String>();
String key1 = "caterpillar";
String key2 = "justin";
map.put(key1, "caterpillar的讯息");
map.put(key2, "justin的讯息");
System.out.println(map.get(key1));
System.out.println(map.get(key2));
3.可以使用values()方法返回一个实作Collection的对象,当中包括所有的「值」对象
.
Map<String, String> map =
new HashMap<String, String>();
map.put("justin", "justin的讯息");
map.put("momor", "momor的讯息");
map.put("caterpillar", "caterpillar的讯息");
Collection collection = map.values();
Iterator iterator = collection.iterator();
while(iterator.hasNext()) {
System.out.println(iterator.next());
}
System.out.println();
4. Map<String, String> map =
new LinkedHashMap<String, String>();
map.put("justin", "justin的讯息");
map.put("momor", "momor的讯息");
map.put("caterpillar", "caterpillar的讯息");
for(String value : map.values()) {
System.out.println(value);
}
get(object key);//根据key值找出对应的value值。
判断键是否存在:containsKey(object key)
判断值是否存在:containsValue(object value)
方法/步骤
1.Map的特性即「键-值」(Key-Value)匹配java.util.HashMap实作了Map界面,
HashMap在内部实作使用哈希(Hash),很快的时间内可以寻得「键-值」匹配.
2. Map<String, String> map =
new HashMap<String, String>();
String key1 = "caterpillar";
String key2 = "justin";
map.put(key1, "caterpillar的讯息");
map.put(key2, "justin的讯息");
System.out.println(map.get(key1));
System.out.println(map.get(key2));
3.可以使用values()方法返回一个实作Collection的对象,当中包括所有的「值」对象
.
Map<String, String> map =
new HashMap<String, String>();
map.put("justin", "justin的讯息");
map.put("momor", "momor的讯息");
map.put("caterpillar", "caterpillar的讯息");
Collection collection = map.values();
Iterator iterator = collection.iterator();
while(iterator.hasNext()) {
System.out.println(iterator.next());
}
System.out.println();
4. Map<String, String> map =
new LinkedHashMap<String, String>();
map.put("justin", "justin的讯息");
map.put("momor", "momor的讯息");
map.put("caterpillar", "caterpillar的讯息");
for(String value : map.values()) {
System.out.println(value);
}
相关文章推荐
- Boost:managed_mapped_file及C++内存分配和对象构造分析
- 利用jxl读取excel合并的单元格的一个小样例
- Tiny6410移植tslib
- [转] 如何把书上的字弄到电脑
- Linux文件类型
- file_operation结构体详解
- Leetcode240: Word Break II
- Akka笔记之Actor简介
- 圆满2015,奋斗2016
- ansible服务模块和组模块使用
- Codeforces Round #339 (Div. 2)-D. Skills
- 转义符大全
- jquery.form.js使用
- ansible服务模块和组模块使用
- Java语言概述
- codeforces 614B(div.2) 模拟
- 手把手教你自制一寸两寸照
- Sed
- win7 c盘越用越少的原因—win7 c盘空间不够的解决方法
- python list循环语句的使用方法