Java——Iterate through a HashMap
2016-04-06 08:34
435 查看
遍历Map
for (String key : map.keySet())
{
// ...
}
//If you only need the values, use values():
for (Object value : map.values())
{
// ...
}
//Finally, if you want both the key and value, use entrySet():
for (Map.Entry<String, Object> entry : map.entrySet())
{
String key = entry.getKey();
Object value = entry.getValue();
// ...
}
//
Iterator it = map.entrySet().iterator();
while (it.hasNext()) {
Map.Entry pair = (Map.Entry)it.next();
System.out.println(pair.getKey() + " = " + pair.getValue());
it.remove(); // avoids a ConcurrentModificationException
}
}
}[/code]
来源:http://stackoverflow.com/questions/1066589/iterate-through-a-hashmap
import java.util.*; public class IterateHashMap { public static void main(String[] args) { Map<String,Object> map=new HashMap<String,Object>(); // If you're only interested in the keys, you can iterate through the [code]keySet()of the map:
for (String key : map.keySet())
{
// ...
}
//If you only need the values, use values():
for (Object value : map.values())
{
// ...
}
//Finally, if you want both the key and value, use entrySet():
for (Map.Entry<String, Object> entry : map.entrySet())
{
String key = entry.getKey();
Object value = entry.getValue();
// ...
}
//
Iterator it = map.entrySet().iterator();
while (it.hasNext()) {
Map.Entry pair = (Map.Entry)it.next();
System.out.println(pair.getKey() + " = " + pair.getValue());
it.remove(); // avoids a ConcurrentModificationException
}
}
}[/code]
来源:http://stackoverflow.com/questions/1066589/iterate-through-a-hashmap
相关文章推荐
- java 搭建webservice服务+testclient測试
- 通过反编译深入理解Java String及intern
- 浅谈JAVA ThreadPoolExecutor(转)
- java.sql.SQLException: 无效的列索引
- Java 面向对象基础语法
- Java 如何有效地避免OOM:善于利用软引用和弱引用
- Java 面向对象基础
- Java克隆
- Java学习笔记3-JDBC事务管理
- Java String.split()用法小结
- springmvc的配置
- Win7下安装配置Java
- 疯狂javaee jsp自定义带属性标签访问数据库java.lang.NullPointerException
- springMVC初探
- SpringMVC学习记录(五)--表单标签
- eclipse安装hibernate
- java-lang-OutOfMemoryError-PermGen-space
- 在VPS上部署javaweb应用
- java原生的zip压缩与解压缩实现
- Intellij Maven创建SpringMVC 框架