GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — HashMap源码分析中put的方法的流程图的疑问 · Issue #608 · Snailclimb/JavaGuide

HashMap源码分析中put的方法的流程图的疑问 #608

New issue
Copy link
New issue
Copy link
Closed
Closed
HashMap源码分析中put的方法的流程图的疑问#608
Copy link
Labels
doc-bugContent errorenhancementNew feature or request or suggestion

Description

@zhongshanhao
zhongshanhao
opened on Dec 31, 2019
Issue body actions

图中若key存在,直接覆盖其节点值,这时不应该将size加1吧?按照源码分析应该直接返回被覆盖之前的值吧?

Activity

  1. LiWenGu commented on Dec 31, 2019

    @LiWenGu
    LiWenGu
    on Dec 31, 2019
    Contributor
    More actions

    直接覆盖之后应该就会 return,不会有后续操作。这个图确实有问题。
    参考 JDK8 HashMap.java 658 行

    Reacted by 峰
  2. zhongshanhao commented on Dec 31, 2019

    @zhongshanhao
    zhongshanhao
    on Dec 31, 2019
    Author
    More actions

    直接覆盖之后应该就会 return,不会有后续操作。这个图确实有问题。
    参考 JDK8 HashMap.java 658 行

    确实,源码实现是直接返回被覆盖的值的。

  3. Snailclimb
    added
    doc-bugContent error
    enhancementNew feature or request or suggestion
    on Jan 22, 2020
  4. softwareCQT commented on Mar 29, 2020

    @softwareCQT
    softwareCQT
    on Mar 29, 2020
    More actions

    如果是putIfAbsent的话才不会被覆盖掉,返回原来的值,主要看那个onlyIfAbsent

  5. hokage123 commented on Jun 10, 2020

    @hokage123
    hokage123
    on Jun 10, 2020
    More actions

    PutIfAbsent Or OnlyIfabsent serve what purpose
    If Put() method work for the key if it is not present
    Otherwise it overrides the value of key.

  6. shahainloong commented on Nov 1, 2020

    @shahainloong
    shahainloong
    on Nov 1, 2020
    Contributor
    More actions

    扩容之后返回null,直接覆盖的话会返回原来的value值。

  7. llnancy commented on Nov 16, 2020

    @llnancy
    llnancy
    on Nov 16, 2020
    More actions

    长度大于8后转换成红黑树那里有问题吧?后续不会执行红黑树插入逻辑,只是长度大于8后会进行树化,节点已经在上一步插入至链表了。

    参考 JDK8 HashMap.java 642 行 -> 执行尾插至链表逻辑
    参考 JDK8 HashMap.java 644 行 -> 判断大于8后执行treeifyBin树化逻辑

  8. sAMs126 commented on Mar 17, 2021

    @sAMs126
    sAMs126
    on Mar 17, 2021
    More actions

    有一个疑问,是否可以这样理解,除了直接追加数组元素 tab[i] = newNode(hash, key, value, null); 这种方式,其他的插入/覆盖方式,都不会对整个数组的长度产生影响,也就无从谈起数组扩容?

    但是 treeifyBin(tab, hash); 方法中的

    if (tab == null || (n = tab.length) < MIN_TREEIFY_CAPACITY) resize();

    难道是仅仅只会在整个数组为空,或者长度小于 64 是进行扩容?

    Replaces all linked nodes in bin at index for given hash unless table is too small, in which case resizes instead.

  9. Snailclimb
    closed this as completedon Jul 22, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    doc-bugContent errorenhancementNew feature or request or suggestion

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions

      You are visitor 01402775 — GeezerVison does not store the pages you look up.
      Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.