如何得到hashmap的key?

marf_cn 2007-12-05 09:04:29
hashmap里提供了get方法可以通过key得到value,但我想得到key应该怎么做?(不是通过value取key,而是直接得到key)
...全文
6165 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
cursor_wang 2007-12-05
  • 打赏
  • 举报
回复
import java.util.*;
class MapTest{
public static void main(String[] args){
HashMap hm=new HashMap();
hm.put("one","aaa");
hm.put("two","bbb");
hm.put("three","ccc");
hm.put("four","ddd");
hm.remove("three");
hm.put("five","eee");
Set set=hm.entrySet();
Iterator it=set.iterator();
while(it.hasNext()){
Map.Entry me=(Map.Entry)it.next();
System.out.println(me.getKey()+":"+me.getValue()+":"+me.hashCode());
}

}
}
HAOSKYZHANG 2007-12-05
  • 打赏
  • 举报
回复
HashMap m = new HashMap();
Set keys = m.keySet(); // 获取主键集合
shan1119 2007-12-05
  • 打赏
  • 举报
回复
	  HashMap hp = new HashMap();
hp.put("1", "2");
System.out.println(hp.keySet());
believefym 2007-12-05
  • 打赏
  • 举报
回复
HashMap.keySet()
然后用iterator遍历即可
zhitaorao 2007-12-05
  • 打赏
  • 举报
回复
import java.util.*;
class MapTest{
public static void main(String[] args){
HashMap hm=new HashMap();
hm.put("one","aaa");
hm.put("two","bbb");
hm.put("three","ccc");
hm.put("four","ddd");
hm.put("five","eee");
Set set=hm.entrySet();
Iterator it=set.iterator();
while(it.hasNext()){
Map.Entry me=(Map.Entry)it.next();
System.out.println(me.getKey()); }

}
}
manqinglb 2007-12-05
  • 打赏
  • 举报
回复
上面那个你把HashMap转为Hashtable就可以了
HashMap没有这个方法
我写错了
manqinglb 2007-12-05
  • 打赏
  • 举报
回复
HashMap hsMap = new HashMap();
hsMap.put("one","aaa");
hsMap.put("two","bbb");

Enumeration eCol = htCol.keys();

while( eCol.hasMoreElements()){
System.out.println(eCol.nextElement());
}

给不给加分!!!!
zazxzx 2007-12-05
  • 打赏
  • 举报
回复
用 keySet() 方法
ftgreat 2007-12-05
  • 打赏
  • 举报
回复
o

62,615

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧