读书人

HashMap所有方法的摘要

发布时间: 2012-10-25 10:58:58 作者: rapoo

HashMap全部方法的摘要

//HashMap全部方法的摘要

public class TestMap7 {
?public static void main(String[] args){
??Map<String,Integer> map=new HashMap<String,Integer>();
??
??map.put("e", 8);
??map.put("b", 67);
??map.put("d", 23);
??map.put("a", 67);
??
//1、?map.entrySet();方法的用法
//??Iterator ite=map.entrySet().iterator();
//??while(ite.hasNext()){
//??Map.Entry<String, Integer>?entry=(Map.Entry<String, Integer>)ite.next();
//????? System.out.print(entry.getKey()+"/");
//????? System.out.println(entry.getValue());
//??}
??
??
//2、??map.clear()与map.isEmpty();的用法
//???? ?System.out.println(map.isEmpty());//答案:false
//????? map.clear();//清空map
//????? System.out.println(map.isEmpty());//答案:true
?????
??
//3、??map.containsKey();map.containsValue();判断key或value是否存在
//????? System.out.println(map.containsKey("a"));//true
//????? System.out.println(map.containsValue(23));//true

??
//???map.get();的用法?
//????? System.out.println(map.get("a"));
//????? map中要是没有这个key,那value就的值为空
//???????? System.out.println(map.get("v"));
??
??
//????? map.keySet();只取map中key的值。
//????? Iterator ite=map.keySet().iterator();
//????? while(ite.hasNext()){
//????? ?String srings=(String)ite.next();
//????? ?System.out.println(srings);
//????? }
??
??
//??? map.remove();的用法
//??? System.out.println(map.remove("a")+"-------------");
//???Iterator ite=map.entrySet().iterator();
//???while(ite.hasNext()){
//???Map.Entry<String, Integer>?entry=(Map.Entry<String, Integer>)ite.next();
//?????? System.out.print(entry.getKey()+"/");
//?????? System.out.println(entry.getValue());
//???}
??
??
//??? map.size();返回map映射的大小。
//?? ?? System.out.println(map.size());
??
??
//??? map.values(); 取出map中values;
//??? Iterator ite=map.values().iterator();
//????????? while(ite.hasNext()){
//??????? ???? Integer integ=(Integer)ite.next();
//??????? ???? System.out.println(integ);
//??????? }
?????
??
?}
}

?

读书人网 >编程

热点推荐