Java集合Map集合 HashMap(常用)方法

Posted lixiansheng

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Java集合Map集合 HashMap(常用)方法相关的知识,希望对你有一定的参考价值。

 

 

HashMap的存值

import java.util.*;

public class test
    public static void main(String[] args) 
        HashMap<Integer, String> map = new HashMap<>();
        map.put(1,"leslie");
        map.put(1,"Jeo");
        map.put(2,"Sezzy");
        map.put(3,"Pit");
        System.out.println(map);
    

注:添加重复的键会被最新添加的覆盖

技术图片

 

HashMap的取值

import java.util.*;

public class test
    public static void main(String[] args) 
        HashMap<Integer, String> map = new HashMap<>();
        map.put(1,"leslie");
        map.put(2,"Sezzy");
        map.put(3,"Pit");
        System.out.println(map.get(3));
        System.out.println(map.get("Sezzy"));
    

技术图片

 

HashMap的判断为空

import java.util.*;

public class test
    public static void main(String[] args) 
        HashMap<Integer, String> map = new HashMap<>();
        System.out.println(map.isEmpty());
        map.put(1,"leslie");
        map.put(2,"Sezzy");
        map.put(3,"Pit");
        System.out.println(map.isEmpty());
    

技术图片

 

HashMap判断是否含有key

import java.util.*;

public class test
    public static void main(String[] args) 
        HashMap<Integer, String> map = new HashMap<>();
        map.put(1,"leslie");
        map.put(2,"Sezzy");
        map.put(3,"Pit");
        System.out.println(map.containsKey(4));
        System.out.println(map.containsKey(2));
    

技术图片

 

 HashMap判断是否含有value

import java.util.*;

public class test
    public static void main(String[] args) 
        HashMap<Integer, String> map = new HashMap<>();
        map.put(1,"leslie");
        map.put(2,"Sezzy");
        map.put(3,"Pit");
        System.out.println(map.containsValue("wang"));
        System.out.println(map.containsValue("Sezzy"));
    

技术图片

 

HashMap删除这个key下的value

import java.util.*;

public class test
    public static void main(String[] args) 
        HashMap<Integer, String> map = new HashMap<>();
        map.put(1,"leslie");
        map.put(2,"Sezzy");
        map.put(3,"Pit");
        System.out.println(map.remove(2));
        System.out.println(map);
    

技术图片

 

HashMap显示所有的value值

import java.util.*;

public class test
    public static void main(String[] args) 
        HashMap<Integer, String> map = new HashMap<>();
        map.put(1,"leslie");
        map.put(2,"Sezzy");
        map.put(3,"Pit");
        System.out.println(map.values());
        System.out.println(map);
    

技术图片

 

HashMap的元素个数

import java.util.*;

public class test
    public static void main(String[] args) 
        HashMap<Integer, String> map = new HashMap<>();
        System.out.println(map.size());
        map.put(1,"leslie");
        map.put(2,"Sezzy");
        map.put(3,"Pit");
        System.out.println(map.size());
    

技术图片

 

HashMap取出全部的key

import java.util.*;

public class test
    public static void main(String[] args) 
        HashMap<Integer, String> map = new HashMap<>();
        System.out.println(map.keySet());
        map.put(1,"leslie");
        map.put(2,"Sezzy");
        System.out.println(map.keySet());
        map.put(3,"Pit");
        System.out.println(map.keySet());
        System.out.println(map);
    

技术图片

 

HashMap替换key的value

import java.util.*;

public class test
    public static void main(String[] args) 
        HashMap<Integer, String> map = new HashMap<>();
        map.put(1,"leslie");
        map.put(2,"Sezzy");
        map.put(3,"Pit");
        System.out.println(map);
        System.out.println(map.replace(3, "Jack"));
        System.out.println(map);
    

技术图片

 

清空这个HashMap

import java.util.*;

public class test
    public static void main(String[] args) 
        HashMap<Integer, String> map = new HashMap<>();
        map.put(1,"leslie");
        map.put(2,"Sezzy");
        map.put(3,"Pit");
        System.out.println(map);
        map.clear();
        System.out.println(map);
    

技术图片

 

HashMap的克隆

import java.util.*;

public class test
    public static void main(String[] args) 
        HashMap<Integer, String> map = new HashMap<>();
        map.put(1,"leslie");
        map.put(2,"Sezzy");
        map.put(3,"Pit");
        System.out.println(map.clone());
        Object clone = map.clone();
        System.out.println(clone);
    

技术图片

 

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

import java.util.*;

public class test
    public static void main(String[] args) 
        HashMap<Integer, String> map = new HashMap<>();
        map.put(1,"leslie");
        map.put(2,"Sezzy");
        map.put(3,"Pit");
        System.out.println(maps);
        System.out.println(map.putIfAbsent(1, "wang"));
        System.out.println(map.putIfAbsent(4, "Lee"));
        System.out.println(map);
    

技术图片

 

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

import java.util.*;

public class test
    public static void main(String[] args) 
        HashMap<Integer, String> map = new HashMap<>();
        map.put(1,"leslie");
        map.put(2,"Sezzy");
        map.put(3,"Pit");
        System.out.println(map);
        map.compute(2, (k, v) -> v == null ? "Suzan" : "Faker");
        System.out.println(map);
    

技术图片

 

以上是关于Java集合Map集合 HashMap(常用)方法的主要内容,如果未能解决你的问题,请参考以下文章

java中HashMap集合的常用方法

JAVA集合04_Map接口概述常用方法排序Hashtable面试题

Java--Map和HashMap基础

JavaMap集合,HashMap,LinkedHashMap,HashTable,Hashmap底层的原理

map集合遍历的几种常用方法

JAVA-初步认识-常用对象API(集合框架-Map集合-hashmap存储自定义对象)