日期:2014-05-20  浏览次数:20901 次

如何得到hashmap的key?
hashmap里提供了get方法可以通过key得到value,但我想得到key应该怎么做?(不是通过value取key,而是直接得到key)

------解决方案--------------------
HashMap.keySet()
然后用iterator遍历即可
------解决方案--------------------
Java code
      HashMap hp = new HashMap();
      hp.put("1", "2");
      System.out.println(hp.keySet());

------解决方案--------------------
HashMap m = new HashMap();
Set keys = m.keySet(); // 获取主键集合

------解决方案--------------------
Java code
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());
        }
        
    }
}

------解决方案--------------------
o
------解决方案--------------------
用 keySet() 方法
------解决方案--------------------
HashMap hsMap = new HashMap();
hsMap.put("one","aaa");
hsMap.put("two","bbb");

Enumeration eCol = htCol.keys();

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

给不给加分!!!!
------解决方案--------------------
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()); }

}
}