I donβt understand why a new tab is needed.
This is not a new table ... this is just a local variable.
I can think of three possible reasons:
The readability suggested by @Bhesh Gurung ... although it hardly matters (IMO) here.
It can reduce (somewhat) the damage caused by the fact that one thread calls clear() , and the second thread performs an update, which can lead to table expansion. But this certainly does not fix the problem, so I would be inclined to dismiss it as meaningless.
This can improve performance; for example, since the optimizer knows that the link in the local variable tab cannot change, it can better optimize checks on the bounds of the array.
Of these, I think the 3rd reason is the most plausible.
(I don't think this has anything to do with the transient modifier. In this case, the modifier is read-only. The HashMap class provides readObject and writeObject , which transient displays.)
source share