hashmap对象的使用
来源:互联网 发布:西安行知小学地址 编辑:程序博客网 时间:2024/04/29 14:53
1111111111111111111111111111111111111111111111111111111
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("map:"+map );
// clear() : 清空HashMap
map.clear();
// isEmpty() : HashMap是否为空
System.out.println((map.isEmpty()?"map is empty":"map is not empty") );
2222222222222222222222222222222222222222222222222222222
for(int i=0;i<10;i++){ //为HashMap存储键值对
map.put(i, i);
}
Set<Integer> keySets = map.keySet(); //获取键 的Set集合
System.out.print("Map键:");
for(Integer keySet:keySets){ //迭代输出键
System.out.print(keySet+" ");
}
Collection<Integer> values = map.values(); //获取HashMap值
System.out.print("Map值:");
for(Integer value:values){ //遍历输出HashMap值
System.out.print(value+" ");
}
HashMap获取键和值
// 新建HashMapHashMap 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("map:"+map );
// clear() : 清空HashMap
map.clear();
// isEmpty() : HashMap是否为空
System.out.println((map.isEmpty()?"map is empty":"map is not empty") );
2222222222222222222222222222222222222222222222222222222
java集合类HashMap获取键和值
Map<Integer,Integer> map = new HashMap<Integer,Integer>(); //创建一个HashMapfor(int i=0;i<10;i++){ //为HashMap存储键值对
map.put(i, i);
}
Set<Integer> keySets = map.keySet(); //获取键 的Set集合
System.out.print("Map键:");
for(Integer keySet:keySets){ //迭代输出键
System.out.print(keySet+" ");
}
Collection<Integer> values = map.values(); //获取HashMap值
System.out.print("Map值:");
for(Integer value:values){ //遍历输出HashMap值
System.out.print(value+" ");
}
0 0
- hashmap对象的使用
- HashMap对象的遍历
- HashMap使用对象作为key
- HashMap、TreeMap对象的遍历
- HashMap的key是对象
- HashMap对象的深层克隆
- HashMap使用的注意事项
- hashmap的使用
- HashMap的使用
- hashmap的使用
- Hashmap的使用
- HashMap的使用
- hashmap的合理使用
- hashMap的使用
- Java HashMap的使用
- HashMap的使用
- HashMap的使用
- HashMap的使用简介
- CF 685A Bear and Reverse Radewoosh
- codeforces-653
- Candies(POJ 3159)(无负权边的带权有向图或无向图的单源最短路)(Dijkstra)
- codeforces 660A Co-prime Array
- 《深入理解Java虚拟机》:类加载和初始化(二)
- hashmap对象的使用
- C# 遇到 R6034 Runtime Error的解决办法
- pyqt4文档阅读(7):QSize
- SpringAOP:为目标类添加新的功能(拓展接口)
- jQuery实现层级展开结构
- 集合包含(30)
- modelsim仿真altera器件
- Dijkstra单源最短路,邻接矩阵形式(模板)
- qt 设置菜单栏颜色