读书人

依据数值取键

发布时间: 2013-04-05 10:24:33 作者: rapoo

根据数值取键
import java.util.HashMap;import java.util.Iterator;import java.util.Map;import java.util.Set;public class TestMap {public static void main(String[] args) {// TODO Auto-generated method stubMap m1 = new HashMap();Map m2 = new HashMap();m1.put("a", new Integer(1));m1.put("b", new Integer(2));m1.put("b", "3");m2.put("f", new Integer(1));m2.put("x", new Integer(2));m2.put("y", new Integer(2));m2.put("z", new Integer(2));m2.put("w", new String("abcd"));//System.out.println(m2.containsKey("q"));//System.out.println(m2.containsValue(new Integer(2)));if(m2.containsValue(new Integer(2))){Set<Map.Entry<Object,Object>> s = m2.entrySet();Iterator i = s.iterator();while(i.hasNext()){Map.Entry<Object,Object> m = (Map.Entry<Object,Object>)i.next();if(m.getValue().equals(new Integer(2))){System.out.print(m.getKey() + " ");}}//System.out.println(m2.entrySet());}else{System.out.println("查无此项");}}}

?

读书人网 >其他相关

热点推荐