Map

来源:互联网 发布:淘宝新店铺如何运营 编辑:程序博客网 时间:2024/06/03 08:01

Map小结:

1.Map是key*value对,创建方法+(增删改查)+3种遍历+1种易错点(返回值是null自动拆装箱)

2. 1.一致性: equals是true,hashcode必须一样,反过来也应遵守,不然影响性能
    2.稳定性:hashcode方法多次调用的数字应该相同,不应该是变化的值,除非equals     比较的属性值发生了改变
3.Map的数值:
 * Capacity:数组大小
 * Initial capacity:初始容量
 * size:数据数量
 * Load factor:加载因子(默认0.75),为防止不断扩容计算,可以加参数HashMap()中

********************************************************************************************

知识点1.(下划线是增删改查)

public class demo3 {
public static void main(String[] args) {
Map<String,Integer> map=new HashMap<String,Integer>();//创建
map.put("语文", 99);
map.put("数学", 98);//增
map.put("英语", 97);
System.out.println(map); 
Integer value=map.put("语文", 90);//改
map.remove("语文");//删
value=map.get("数学");//查


//遍历key
Set<String> keySet=map.keySet();
for(String str:keySet){
System.out.println(str);
}
//遍历key-value
Set<Entry<String,Integer>> entrySet=map.entrySet();
for(Entry<String,Integer> e:entrySet){
String str=e.getKey();
Integer i=e.getValue();
System.out.println(str+i);
}
//遍历value
Collection<Integer> c=map.values();
for(Integer i:c){
System.out.println(i);
}
}
}

原创粉丝点击