• Java核心知识:java集合之Map集合


    介绍

    Map集合将键映射到值得对象,一个映射不能包含重复的键,每个键最多映射一个值。

    Map与Collection区别:

    1.Map是双列集合,Collection是单列集合

    2.Map的每个元素由键值对组成,是成对出现的,Collection元素是孤立的

    3.Map的key和value的数据类型可以不同

    常用子类

    1.HashMap

    基于哈希表的Map接口实现,键值允许为null。无序。不同步多线程。

    2.LinkedHashMap

    是HashMap的子类。基于哈希表+链表。有序,保证迭代顺序。

    3.HashTable

    此类实现一个哈希表,将键映射到值,键值不允许为null。同步单线程。

    Map接口常用方法

    增删改查

    public V put(K key, V value): 把指定的键与指定的值添加到Map集合中。键不重复返回null,重复返回新的value值。

    public V remove(Object key): 把指定的键 所对应的键值对元素 在Map集合中删除,返回被删除元素的值。

    public V get(Object key): 根据指定的键,在Map集合中获取对应的值。

    boolean containsKey(Object key): 判断集合中是否包含指定的键。

    public Set keySet(): 获取Map集合中所有的键,存储到Set集合中。

    public Set> entrySet(): 获取到Map集合中所有的键值对对象的集合(Set集合)。

    示例:

     public void testHashMap() {
         Map<String, String> map = new HashMap<>(4);
         map.put("1", "a");
         map.put("2", "b");
         map.put("3", "c");
         map.put("4", "d");
     
         System.out.println("----- map.keySet()-----");
         //获取所有的 key,根据 key 取出对应的value
         for (String key : map.keySet()) {
             System.out.println("key:" + key + ",value:" + map.get(key));
         }
         System.out.println("-----获取map种所有的value:map.values()------");
         //遍历所有的value
         for (String value : map.values()) {
             System.out.println("value:" + value);
         }
         System.out.println("-----获取键值对:map.entrySet()-----");
         //取出对应的 key,value 键值对,容量大时推荐使用
         for (Map.Entry<String, String> entry : map.entrySet()) {
             System.out.println("键值对:" + entry);
             //获取 键值对的 key
             System.out.println("key:" + entry.getKey());
             //获取 键值对的 value
             System.out.println("value:" + entry.getValue());
         }
     
         System.out.println("----- 通过 Map.entrySet使用iterator遍历 key 和 value -----");
         Iterator<Map.Entry<String, String>> iterator = map.entrySet().iterator();
         while (iterator.hasNext()) {
             Map.Entry<String, String> entry = iterator.next();
             System.out.println("key:" + entry.getKey() + ",value:" + entry.getValue());
         }
     
         System.out.println("----- map.forEach JDK1.8 新特性 -----");
         map.forEach((key, value) -> {
             System.out.println("key=" + key + ",value=" + value);
         });
     }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39

    注意:存储自定义属性时,必须实现hashCode和equals方法。

  • 相关阅读:
    瑞芯微RK3568|SDK开发之环境安装及编译操作
    Mybiosource丨Mybiosource重组病毒果蝇蛋白wntless方案
    Docker入门学习笔记
    ThingsBoard IoT Gateway 实战(二)- 通过 Request Connector 获取天气
    【Detectron2】代码库学习-1. 概览
    Linux: Security: sudoers 语法错误
    Netty 核心模块组件
    【App测试】adb三大连接方式-夜神模拟器+真机+android真机(详细步骤)
    PolarDB for PostgreSQL透明加密
    如何将Python程序打包并保护源代码
  • 原文地址:https://blog.csdn.net/qq_43745578/article/details/126092828