//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);
// }
}
}
本文详细介绍了HashMap类的各种方法,包括获取映射集、清除映射、判断键值是否存在、获取值、获取键集、移除元素、获取映射大小、获取值集合等,并通过实例演示了各方法的用法。
488

被折叠的 条评论
为什么被折叠?



