关闭

Map / HashMap 获取Key值的方法

735人阅读 评论(0) 收藏 举报
分类:

方法1:keySet()

HashMap hashmp = ne HashMap();
hashmp.put("aa", "111");
Set set = hashmp.keySet();
Iterator iter = set.iterator();
while (iter.hasNext()) {
String key = (String) iter.next();
// printkey
}
// traverse
for (String key : list.get(pos).keySet() ) {
myKey = key;
}


方法2:entrySet()
HashMap map;
Iterator i = map.entrySet().iterator();
while (i.hasNext()) {
Object obj = i.next();
String key = obj.toString();
}
// or
while (i.hasNext()) {
Entry entry = (java.util.Map.Entry)it.next();
entry.getkey();
entry.getValue();
}


方法1:keySet()
HashMap hashmp = ne HashMap();
hashmp.put("aa", "111");
Set set = hashmp.keySet();
Iterator iter = set.iterator();
while (iter.hasNext()) {
String key = (String) iter.next();
// printkey
}
// traverse
for (String key : list.get(pos).keySet() ) {
myKey = key;
}


方法2:entrySet()
HashMap map;
Iterator i = map.entrySet().iterator();
while (i.hasNext()) {
Object obj = i.next();
String key = obj.toString();
}
// or
while (i.hasNext()) {
Entry entry = (java.util.Map.Entry)it.next();
entry.getkey();
entry.getValue();
}

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:202456次
    • 积分:2963
    • 等级:
    • 排名:第12362名
    • 原创:21篇
    • 转载:451篇
    • 译文:0篇
    • 评论:11条