个人博客:The Blog Of WaiterXiaoYY 欢迎来互相交流学习。
方法摘要
HashMap 是平时我们进行索引时用的比较多的一种储存结构,
HashMap 是继承 AbstractMap 接口和实现了Map接口的类,
常用的方法有:
1. V put(key, value) //在此映射中关联指定值和指定键
2. V get(object key) //获取指定键所映射的值
3. boolean isEmpty() //如果此映射不包含键 - 值映射关系,则返回 true;
4. boolean constainsKey(object key) //如果此映射中包含指定键的映射关系,则返回true;
5. boolean constainsValue(object value) //如果此映射将一个或者多个键映射到指定值,则返回true;
6. int size() //返回此映射中的键 - 值的映射关系数
7. V remove() //从此映射中移除指定键的映射关系(如果存在)
8. V replace(key, value) //替换指定键原本的值为value
9. Set<K> keySet() //返回此映射中所有的键
10. void clear() //从此映射中移除所有的映射关系
11. Object clone() //克隆此映射
方法应用
//关联指定值和指定键
public static void main(String[] args) {
//向map中添加值(返回这个key以前的值,如果没有返回null)
HashMap<String, Integer> map=new HashMap<>();
System.out.println(map.put("demo", 1));//null
System.out.println(map.put("demo", 2));//1
}
//获取指定键所映射的值
public static void main(String[] args) {
HashMap<String, Integer> map=new HashMap<>();
map.put("DEMO", 1);
System.out.println(map.get("kkk"));//null
System.out.println(map.get("DEMO"));//1
}
//判断此映射是否包含键 - 值映射关系,找到则返回true
public static void main(String[] args) {
HashMap<String, Integer> map=new HashMap<>();
System.out.println(map.isEmpty());//true
map.put("demo", 1);
System.out.println(map.isEmpty());//false
}
//如果此映射中包含指定键的映射关系,则返回true;
public static void main(String[] args) {
HashMap<String, Integer> map=new HashMap<>();
System.out.println(map.containsKey("demo"));//false
map.put("demo", 1);
System.out.println(map.containsKey("demo"));//true
}
//如果此映射将一个或者多个键映射到指定值,则返回true;
public static void main(String[] args) {
HashMap<String, Integer> map=new HashMap<>();
System.out.println(map.containsValue(1));//false
map.put("demo", 1);
System.out.println(map.containsValue(1));//true
}
//返回此映射中的键 - 值的映射关系数
public static void main(String[] args) {
HashMap<String, Integer> map=new HashMap<>();
System.out.println(map.size());//0
map.put("demo", 1);
System.out.println(map.size());//1
map.put("kkk", 2);
System.out.println(map.size());//2
}
//从此映射中移除指定键的映射关系(如果存在)
public static void main(String[] args) {
HashMap<String, Integer> map=new HashMap<>();
System.out.println(map.remove("1"));//null
map.put("demo", 2);
System.out.println(map.remove("demo"));//2(删除的值)
}
//替换指定键原本的值为value,返回原本的对应的值
public static void main(String[] args) {
HashMap<String, Integer> map=new HashMap<>();
map.put("demo", 1);
map.put("kkk", 2);
System.out.println(map);//{demo=1, kkk=2}
System.out.println(map.replace("kkk", 1));//2
System.out.println(map);//{demo=1, kkk=1}
}
//返回此映射中所有的键
public static void main(String[] args) {
HashMap<String, Integer> map=new HashMap<>();
System.out.println(map.keySet());//[]
map.put("demo", 1);
System.out.println(map.keySet());//[demo]
map.put("kkk", 2);
System.out.println(map.keySet());//[demo, kkk]
}
//从此映射中移除所有的映射关系
public static void main(String[] args) {
HashMap<String, Integer> map=new HashMap<>();
map.put("demo", 1);
map.put("kkk", 2);
System.out.println(map);//{demo=1, kkk=2}
map.clear();//2
System.out.println(map);//{}
}
//克隆此映射
public static void main(String[] args) {
HashMap<String, Integer> map=new HashMap<>();
map.put("demo", 1);
map.put("kkk", 2);
System.out.println(map.clone());//{demo=1, kkk=2}
Object clone = map.clone();
System.out.println(clone);//{demo=1, kkk=2}
}