HashMap的四种遍历方式
来源:互联网 发布:在端口1433连接失败 编辑:程序博客网 时间:2024/06/06 02:00
package collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
/**
* 遍历HanhMap
* @author Wanglei
* @Date 2016年8月18日
*/
public class ErgodicHashMap {
public static void main(String[] args) {
// TODO Auto-generated method stub
Map<String,Integer> map = new HashMap<String,Integer>();
map.put("1", 100);
map.put("2", 200);
map.put("3", 300);
//ergodic1(map);
// ergodic2(map);
//ergodic3(map);
ergodic4(map);
}
/**
* 遍历方法1
* 通过Map.keySet遍历key和value
*/
public static void ergodic1(Map<String,Integer> map){
if(map != null){
for(String key:map.keySet()){
System.out.println("key="+key+", value="+map.get(key));
}
}
}
/**
* 遍历方法2,不能遍历key
* 通过Map.values()遍历所有的value,但不能遍历key
*/
public static void ergodic2(Map<String,Integer> map){
for(Integer i : map.values()){
System.out.println(i);
}
}
/**
* 遍历方法3
* 通过Map.entrySet使用iterator遍历key和value
*/
public static void ergodic3(Map<String,Integer> map){
Iterator<Map.Entry<String,Integer>> it = map.entrySet().iterator();
while(it.hasNext()){
Map.Entry<String,Integer> entry = it.next();
System.out.println("key="+entry.getKey()+", value="+entry.getValue());
}
}
/**
* 遍历方法4
* 推荐,尤其容量大时
* 通过Map.entrySet遍历key和value
*/
public static void ergodic4(Map<String,Integer> map){
for(Map.Entry<String,Integer> entry : map.entrySet()){
System.out.println("key="+entry.getKey()+", value="+entry.getValue());
}
}
}
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
/**
* 遍历HanhMap
* @author Wanglei
* @Date 2016年8月18日
*/
public class ErgodicHashMap {
public static void main(String[] args) {
// TODO Auto-generated method stub
Map<String,Integer> map = new HashMap<String,Integer>();
map.put("1", 100);
map.put("2", 200);
map.put("3", 300);
//ergodic1(map);
// ergodic2(map);
//ergodic3(map);
ergodic4(map);
}
/**
* 遍历方法1
* 通过Map.keySet遍历key和value
*/
public static void ergodic1(Map<String,Integer> map){
if(map != null){
for(String key:map.keySet()){
System.out.println("key="+key+", value="+map.get(key));
}
}
}
/**
* 遍历方法2,不能遍历key
* 通过Map.values()遍历所有的value,但不能遍历key
*/
public static void ergodic2(Map<String,Integer> map){
for(Integer i : map.values()){
System.out.println(i);
}
}
/**
* 遍历方法3
* 通过Map.entrySet使用iterator遍历key和value
*/
public static void ergodic3(Map<String,Integer> map){
Iterator<Map.Entry<String,Integer>> it = map.entrySet().iterator();
while(it.hasNext()){
Map.Entry<String,Integer> entry = it.next();
System.out.println("key="+entry.getKey()+", value="+entry.getValue());
}
}
/**
* 遍历方法4
* 推荐,尤其容量大时
* 通过Map.entrySet遍历key和value
*/
public static void ergodic4(Map<String,Integer> map){
for(Map.Entry<String,Integer> entry : map.entrySet()){
System.out.println("key="+entry.getKey()+", value="+entry.getValue());
}
}
}
0 0
- HashMap的四种循环遍历方式
- 遍历HashMap的四种方式
- HashMap的四种遍历方式
- Java -- HashMap的四种遍历方式
- 遍历HashMap的四种方式
- 遍历Hashmap的四种方式
- hashmap四种遍历方式
- Java中HashMap遍历的四种方式
- Java中HashMap遍历的四种方式
- Java中HashMap遍历的四种方式
- Java中HashMap遍历的四种方式
- HashMap的四种遍历
- hashMap的四种遍历
- 遍历HashMap的四种
- HashMap的遍历方式
- HashMap的遍历方式
- HashMap的遍历方式
- 遍历HashMap的四种方法
- 查端口和释放端口一些命令
- 关于volatile和cas
- Bootstrap学习(二)——Bootstrap 布局组件
- iOS APP提交审核,关于第三方登录被拒的那些事儿
- Sphinx使用及近实时索引实现
- HashMap的四种遍历方式
- 两年多工作心得和体会
- bzoj1996(区间dp)
- AOE网上的关键路径(拓扑排序+SPFA算法)
- 规则平台中的primary_tag要与globle中的维度的字段名相等
- linux中命令 which/whereis/locate/find 的使用总结(总结+转载)
- HTML之Position用法
- java.lang.UnsatisfiedLinkError
- MK文件示例