HashMap常用方法

Hashmap的存值:
public static void main(String[] args) {
         ///*Integer*/map.put("1", 1);//向map中添加值(返回这个key以前的值,如果没有返回null)
         HashMap<String, Integer> map=new HashMap<>();
         System.out.println(map.put("1", 1));//null
         System.out.println(map.put("1", 2));//1
}
Hashmap的取值:
public static void main(String[] args) {
        HashMap<String, Integer> map=new HashMap<>();
        map.put("DEMO", 1);
        /*Value的类型*///得到map中key相对应的value的值
        System.out.println(map.get("1"));//null
        System.out.println(map.get("DEMO"));//1
}

Hashmap的判断为空:

public static void main(String[] args) {
        HashMap<String, Integer> map=new HashMap<>();
        /*boolean*///判断map是否为空
        System.out.println(map.isEmpty());//true
        map.put("DEMO", 1);
        System.out.println(map.isEmpty());//false
}

Hashmap判断是否含有key:

public static void main(String[] args) {
        HashMap<String, Integer> map=new HashMap<>();
        /*boolean*///判断map中是否存在这个key
        System.out.println(map.containsKey("DEMO"));//false
        map.put("DEMO", 1);
        System.out.println(map.containsKey("DEMO"));//true
}

Hashmap判断是否含有value:

public static void main(String[] args) {
        HashMap<String, Integer> map=new HashMap<>();
        /*boolean*///判断map中是否存在这个value
        System.out.println(map.containsValue(1));//false
        map.put("DEMO", 1);
        System.out.println(map.containsValue(1));//true
}

Hashmap删除这个key值下的value:

public static void main(String[] args) {
        HashMap<String, Integer> map=new HashMap<>();
        /*Integer*///删除key值下的value
        System.out.println(map.remove("1"));//null
        map.put("DEMO", 2);
        System.out.println(map.remove("DEMO"));//2(删除的值)
}

Hashmap显示所有的value值:

public static void main(String[] args) {
        HashMap<String, Integer> map=new HashMap<>();
        /*Collection<Integer>*///显示所有的value值
        System.out.println(map.values());//[]
        map.put("DEMO1", 1);
        map.put("DEMO2", 2);
        System.out.println(map.values());//[1, 2]
}

Hashmap的元素个数:

public static void main(String[] args) {
        HashMap<String, Integer> map=new HashMap<>();
        /*int*///显示map里的值得数量
        System.out.println(map.size());//0
        map.put("DEMO1", 1);
        System.out.println(map.size());//1
        map.put("DEMO2", 2);
        System.out.println(map.size());//2
}

 

Hashmap删除这个key值下的value:

public static void main(String[] args) {
        HashMap<String, Integer> map=new HashMap<>();
        /*SET<String>*///显示map所有的key
        System.out.println(map.keySet());//[]
        map.put("DEMO1", 1);
        System.out.println(map.keySet());//[DEMO1]
        map.put("DEMO2", 2);
        System.out.println(map.keySet());//[DEMO1, DEMO2]
}

 

Hashmap显示所有的key和value:

public static void main(String[] args) {
        HashMap<String, Integer> map=new HashMap<>();
        /*SET<map<String,Integer>>*///显示所有的key和value
        System.out.println(map.entrySet());//[]
        map.put("DEMO1", 1);
        System.out.println(map.entrySet());//[DEMO1=1]
        map.put("DEMO2", 2);
        System.out.println(map.entrySet());//[DEMO1=1, DEMO2=2]
}

 

Hashmap添加另一个同一类型的map下的所有制:

public static void main(String[] args) {
        HashMap<String, Integer> map=new HashMap<>();
        HashMap<String, Integer> map1=new HashMap<>();
        /*void*///将同一类型的map添加到另一个map中
        map1.put("DEMO1", 1);
        map.put("DEMO2", 2);
        System.out.println(map);//{DEMO2=2}
        map.putAll(map1);
        System.out.println(map);//{DEMO1=1, DEMO2=2}
}

 

Hashmap删除这个key和value:

public static void main(String[] args) {
        HashMap<String, Integer> map=new HashMap<>();
        /*boolean*///删除这个键值对
        map.put("DEMO1", 1);
        map.put("DEMO2", 2);
        System.out.println(map);//{DEMO1=1, DEMO2=2}
        System.out.println(map.remove("DEMO2", 1));//false
        System.out.println(map.remove("DEMO2", 2));//true
        System.out.println(map);//{DEMO1=1}
}

 

Hashmap替换这个key的value:(java8)

public static void main(String[] args) {
        HashMap<String, Integer> map=new HashMap<>();
        /*value*///判断map中是否存在这个key
        map.put("DEMO1", 1);
        map.put("DEMO2", 2);
        System.out.println(map);//{DEMO1=1, DEMO2=2}
        System.out.println(map.replace("DEMO2", 1));//2
        System.out.println(map);//{DEMO1=1, DEMO2=1}
}

清空这个hashmap:

public static void main(String[] args) {
        HashMap<String, Integer> map=new HashMap<>();
        /*void*///清空map
        map.put("DEMO1", 1);
        map.put("DEMO2", 2);
        System.out.println(map);//{DEMO1=1, DEMO2=2}
        map.clear();//2
        System.out.println(map);//{}
}

Hashmap的克隆:

public static void main(String[] args) {
        HashMap<String, Integer> map=new HashMap<>();
        /*object*///克隆这个map
        map.put("DEMO1", 1);
        map.put("DEMO2", 2);
        System.out.println(map.clone());//{DEMO1=1, DEMO2=2}
        Object clone = map.clone();
        System.out.println(clone);//{DEMO1=1, DEMO2=2}
}

 如果当前 Map 不存在键 key 或者该 key 关联的值为 null,那么就执行 put(key, value);否则,便不执行 put 操作:(java8新增方法)

public static void main(String[] args) {
		HashMap<String, Integer> map=new HashMap<>();
		/*boolean*///判断map中是否存在这个key
		map.put("DEMO1", 1);
		map.put("DEMO2", 2);
		System.out.println(map);//{DEMO1=1, DEMO2=2}
		System.out.println(map.putIfAbsent("DEMO1", 12222));//1
		System.out.println(map.putIfAbsent("DEMO3", 12222));//null
		System.out.println(map);//{DEMO1=1, DEMO2=2}
}

 如果当前 Map 的value为xx时则值为xx否则为xx:(java8新增方法)compute 方法更适用于更新 key 关联的 value 时,新值依赖于旧值的情况 

public static void main(String[] args) {
        HashMap<String, Integer> map=new HashMap<>();
        /*boolean*///当这个value为null时为1,否则为3
        map.put("DEMO1", 1);
        map.put("DEMO2", 2);
        System.out.println(map);//{DEMO1=1, DEMO2=2}
        map.compute("DEMO2", (k,v)->v==null?1:3);
        System.out.println(map);//{DEMO1=1, DEMO2=3}
}

如果当前 Map 的value为xx时则值为xx否则为xx:(java8新增方法)

如果当前 Map 的value为xx时则值为xx否则为xx:(java8新增方法)

如果当前 Map 的value为xx时则值为xx否则为xx:(java8新增方法)

如果当前 Map 的value为xx时则值为xx否则为xx:(java8新增方法)

如果当前 Map 的value为xx时则值为xx否则为xx:(java8新增方法)

 

java8新增方法

方法详解
/**/map.computeIfAbsent(key, mappingFunction);
/**/map.computeIfPresent(key, remappingFunction);
/**/map.forEach());
/**/map.merge(key, value, remappingFunction);
/**/map.getOrDefault(key, defaultValue);

  • 7
    点赞
  • 27
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值