HashMap常用的方法

个人博客: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}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值