Search code examples
javahashmapentryset

Why entryset in HashMap calls entryset0?


Why entryset in HashMap calls entryset0 instead of directly writing

Set <Map.Entry <K, V >> es = entrySet; 
! return es = null es: (entrySet = new EntrySet ());

In the writeObject method directly calls entrySet0, is that because of the reason that the entrySet method can be overridden?


Solution

  • Your explanation is the most plausible. This is to insulate the implementation of HashMap from the effects of someone overriding the entrySet() method in a subtype.

    The only other explanation I can think of is that this is left over from some older version of the class, and I wouldn't expect that the Java developers would have let that happen here. (This is code has lots of eyeballs on it ...)

    Either way, it probably makes no difference to performance. The JIT compiler is likely to inline the call ... except in the case where there is a subclass of HashMap that does override entrySet().