如何在不使用Iterator的情况下从Hashmap获取值? [英] How to get the values from the Hashmap without using Iterator?

查看:81
本文介绍了如何在不使用Iterator的情况下从Hashmap获取值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Iterator iterator = map.entrySet().iterator();
while (iterator.hasNext()) 
{
    Map.Entry mapEntry = (Map.Entry) iterator.next();
System.out.println("The key is: " + mapEntry.getKey() + ",value is :" + mapEntry.getValue());

}

这是我的代码.现在,我不想使用Iterator来获取值. 请帮助我找到最佳解决方案.

This is my code. Now i don't want to use Iterator to get the values. Please help me to find best solution.

推荐答案

Map<String, Object> map = .....;//Initialization here
for (String key : map.keySet()) {
    // write your code here
}

//如果您仅使用地图的键

//If you are just using keys of the Map

for (Object value : map.values()) {
     // write your code here
}

//如果您仅使用地图中的值

//If you are just using values from your Map

for (Map.Entry<String, Object> entry : map.entrySet()) {
    String key = entry.getKey();
    Object value = entry.getValue();
    // you code here
}

//如果要同时使用键和值

//If you want both Keys and values

//全部都不使用Map的迭代器

//All are without using Iterator of the Map

这篇关于如何在不使用Iterator的情况下从Hashmap获取值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆