Map源码解读

本文详细介绍了Java HashMap的数据结构,包括内部的Node类和loadFactor设置,以及不同的遍历方法。重点讲解了如何通过Entry集合和迭代器实现遍历,并揭示了元素从树形转为链表的机制。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一、Map

key不允许重复,和HashSet一样。 可以为null,但是只能包含一个null
value允许重复,可以包含多个null

二、HashMap

  • 具体结构在前面HashSet已经介绍完毕
  • 是线程不安全的

1. 数据结构

 private static void method01() {
     HashMap<String, String> map = new HashMap<>();
     map.put("name", "erick");

     /*是为了更好的遍历,底层是
     * 1. 内部类Map.Entry<String, String>
        static class Node<K,V> implements Map.Entry<K,V>
       2. 将entry放在set中
       3. 将node节点的引用放在set中*/
     Set<Map.Entry<String, String>> entries = map.entrySet();
     for (Map.Entry<String, String> entry : entries) {
         String key = entry.getKey();
         String value = entry.getValue();
     }
 }

在这里插入图片描述

static final float DEFAULT_LOAD_FACTOR = 0.75f;

public HashMap() {
    this.loadFactor = DEFAULT_LOAD_FACTOR; // all other fields defaulted
}
  static class Node<K,V> implements Map.Entry<K,V> {
        final int hash;
        final K key;
        V value;
        Node<K,V> next;

        Node(int hash, K key, V value, Node<K,V> next) {
            this.hash = hash;
            this.key = key;
            this.value = value;
            this.next = next;
        }
# 存放的元素是node
- Node<K,V> implements Map.Entry<K,V> 

- k为存入的key, v为对应的value

- 某个节点的元素,如果是之前是树,后续当树的节点减少到6个,就会
  重新变成链表

- 重复元素时候,替换对应的value,key不变

2. 遍历方式

package com.erick.feature.collection.d02;

import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

public class Demo05 {
    public static void main(String[] args) {
        HashMap<String, String> map = new HashMap<>();
        map.put("name", "erick");
        map.put("age", "11");
        method03(map);
    }

    private static void method01(Map<String, String> map) {
        Set<Map.Entry<String, String>> entries = map.entrySet();
        /*1. 通过获取到entry集合,增强for*/
        for (Map.Entry<String, String> entry : entries) {
            String key = entry.getKey();
            String value = entry.getValue();
        }
    }

    private static void method02(Map<String, String> map) {
        Set<Map.Entry<String, String>> entries = map.entrySet();
        /*2. 通过获取到entry集合,迭代器*/
        Iterator<Map.Entry<String, String>> iterator = entries.iterator();
        while (iterator.hasNext()) {
            Map.Entry<String, String> next = iterator.next();
            String key = next.getKey();
            String value = next.getValue();
        }
    }

    private static void method03(Map<String, String> map) {
        /*3. 获取到所有的value,然后来遍历*/
        Collection<String> values = map.values();
        for (String value : values) {
            System.out.println(value);
        }
    }

    private static void method04(Map<String, String> map) {
        /*4. 获取到所有的value,然后来遍历*/
        Collection<String> values = map.values();
        Iterator<String> iterator = values.iterator();
        while (iterator.hasNext()){
            System.out.println(iterator.next());
        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值