Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8437323
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T07:23:56+00:00 2026-06-10T07:23:56+00:00

I saw java hash map ,the clear method,like this: public void clear() { modCount++;

  • 0

I saw java hash map ,the clear method,like this:

public void clear() {
    modCount++;
    Entry[] tab = table;
    for (int i = 0; i < tab.length; i++)
        tab[i] = null;
    size = 0;
}

I don’t understand,why to take new tab to clear.

Why not use table to clear?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T07:23:58+00:00Added an answer on June 10, 2026 at 7:23 am

    I don’t understand, why new tab to clear.

    It is not a new table … it is just a local variable.

    I can think of three possible reasons:

    • Readability, as suggested by @Bhesh Gurung … though it hardly makes a difference here (IMO).

    • It might mitigate (somewhat) the damage caused if one thread calls clear() while a second thread does an update that might cause the table to be expanded. But it certainly doesn’t fix the problem, so I’d be inclined to dismiss this as being nonsensical.

    • It might improve performance; e.g. since the optimizer knows that the reference in the local variable tab cannot change, it can optimize the array bounds checks better.

    Of these, I think that the 3rd reason is the most plausible.

    (I don’t think it is anything to do with the transient modifier. In this case, the modifier is only there for readability. The HashMap class provides readObject and writeObject which renders the transient modifier moot.)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Should I implement named parameters in Java using Hash tables? I saw this entry:
I saw a java function that looked something like this- public static<T> foo() {...}
I saw someone initialize and array like this in java int[] s; s =
In this Java code: public class Main { public static void main(String[] args) {
I saw a Java example that had a main method labeled as synchronized, calling
When debugging Java code using Eclipse, for collection variables, I saw the modcount member.
Was recently reviewing some Java Swing code and saw this: byte[] fooReference; String getFoo()
I saw this list of major version numbers for Java in another post: Java
I was reading java bytecode and saw this: getfield #5 (Field java.lang.String name) What
I just saw this data-structure on the Java 6 API and I'm curious about

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.