Map集合
来源:互联网 发布:unity5.5.1f1 for mac 编辑:程序博客网 时间:2024/05/21 18:31
package cn.itcast.map.demo;
import java.util.Map;
import java.util.Set;
import java.util.Iterator;
import java.util.HashMap;
public class MapDemo {
public static void main(String[] args) {
// TODO Auto-generated method stub
Map<Integer, String> map = new HashMap<Integer, String>();
method(map);
}
public static void method(Map<Integer, String> map) {
// TODO Auto-generated method stub
map.put(1, "ahs");
map.put(2, "fgd");
map.put(3, "rter");
map.put(4, "grtes");
// 方法一:取出map中的所有元素原理
// 通过keySet方法获取Map中所有键的Set集合,再通过Set的迭代器取所有的键
// 在对每一个键用map集合的get方法获取其对应的值。
Set<Integer> keyset = map.keySet();
Iterator<Integer> it = keyset.iterator();
//前两句等同Iterator<Integer> it = map.keySet().iterator();
while (it.hasNext()) {
Integer key = it.next();
String value = map.get(key);
System.out.println(key + ":" + value);
}
/*
* 方法二:通过MAp转换成Set
* 通过entrySet方法将键值的映射关系作为对象存储到set集合中,
* 而这个映射关系类型就是Map.Entry类型
*/
Set<Map.Entry<Integer,String>> entrySet=map.entrySet();
Iterator<Map.Entry<Integer,String>> itt = entrySet.iterator();
while (itt.hasNext()) {
Map.Entry<Integer,String> me=itt.next();
Integer key = me.getKey();
String value = me.getValue();
System.out.println(key + ":" + value);
}
}
}
- Map 集合
- Map集合
- Map集合
- map集合
- Map集合
- map集合
- Map集合
- Map集合
- Map集合
- Map集合
- Map集合
- map集合
- Map集合
- Map集合
- Map集合
- Map集合
- Map集合
- Map集合
- 最新Androstudio导入第三方库
- 面试总结10
- 28. Element nodeValue 属性
- svn cleanup failed–previous operation has not finished 解决方法
- Python排序,针对列表,字典
- Map集合
- 1049:Climbing Worm
- 分解字符串
- js中如何将GBK的英文转为普通英文, 全角转为半角
- define和inline关键字比较
- 29. Element ownerDocument 属性
- iOS Layer层相关,图片裁剪与形变
- bfs(广搜)
- JNI高级教程之数据类型转换