HashMap初始容量总结,彻底吃透位运算,简历立马多项熟练

 
场景描述我们在日常学习和研发中,经常会接触一些底层的源码,有些同学在遇到位运算(提高系统的运行效率)实现的方法时,读起来就有些吃力了,例如HashMap类中的tableSizeFor(int cap)方法,源代码如下 。
/*** The maximum capacity, used if a higher value is implicitly specified* by either of the constructors with arguments.* MUST be a power of two <= 1<<30.*/static final int MAXIMUM_CAPACITY = 1 << 30;/*** Returns a power of two size for the given target capacity.*/static final int tableSizeFor(int cap) {int n = cap - 1;n |= n >>> 1;n |= n >>> 2;n |= n >>> 4;n |= n >>> 8;n |= n >>> 16;return (n < 0) ? 1 : (n >= MAXIMUM_CAPACITY) ? MAXIMUM_CAPACITY : n + 1;}当我们使用HashMap集合没有指定初始容量时默认是16,如果给定了一个初始容量,tableSizeFor(int cap)方法会返回一个等于2的n次方的数来作为集合的初始容量 。如果用户给定的初始容量就等于2的n次方,那么返回的这个数就等于用户给定值,否则将会返回一个大于且最接近用户给定值的2的n次方的数来作为HashMap集合的初始容量 。
案例演示
为了演示效果我这里使用反射的方式直接调用HashMap类里的tableSizeFor(int cap)方法测试容量的返回值 。有对反射不是很理解的同学可以将源码复制出来测试也可以在这个方法上添加断点debug调试 。
代码示例假设用户给定初始容量 cap=7
import JAVA.lang.reflect.Method;public class Demo {public static void main(String[] args) throws Exception {Class aClass = Class.forName("java.util.HashMap");Object instance = aClass.newInstance();Method tableSizeFor = aClass.getDeclaredMethod("tableSizeFor", int.class);//设置目标类中的tableSizeFor方法可访问tableSizeFor.setAccessible(true);//假设用户给定的初始值时cap=7Object invoke = tableSizeFor.invoke(instance, 7);//打印返回值System.out.println("tableSizeFor方法返回值:"+invoke);}}运行结果F:softwareJavajdk1.8.0_121binjava.exe "-javaagent:D:IntelliJ IDEA 2019.1.4libidea_rt.jar=51583:D:IntelliJ IDEA 2019.1.4bin" -Dfile.encoding=UTF-8 -classpath F:softwareJavajdk1.8.0_121jrelibcharsets.jar;F:softwareJavajdk1.8.0_121jrelibdeploy.jar;F:softwareJavajdk1.8.0_121jrelibextaccess-bridge-64.jar;F:softwareJavajdk1.8.0_121jrelibextcldrdata.jar;F:softwareJavajdk1.8.0_121jrelibextDNSns.jar;F:softwareJavajdk1.8.0_121jrelibextjaccess.jar;F:softwareJavajdk1.8.0_121jrelibextjfxrt.jar;F:softwareJavajdk1.8.0_121jrelibextlocaledata.jar;F:softwareJavajdk1.8.0_121jrelibextnashorn.jar;F:softwareJavajdk1.8.0_121jrelibextsunec.jar;F:softwareJavajdk1.8.0_121jrelibextsunjce_provider.jar;F:softwareJavajdk1.8.0_121jrelibextsunmscapi.jar;F:softwareJavajdk1.8.0_121jrelibextsunpkcs11.jar;F:softwareJavajdk1.8.0_121jrelibextzipfs.jar;F:softwareJavajdk1.8.0_121jrelibjavaws.jar;F:softwareJavajdk1.8.0_121jrelibjce.jar;F:softwareJavajdk1.8.0_121jrelibjfr.jar;F:softwareJavajdk1.8.0_121jrelibjfxswt.jar;F:softwareJavajdk1.8.0_121jrelibjsse.jar;F:softwareJavajdk1.8.0_121jrelibmanagement-agent.jar;F:softwareJavajdk1.8.0_121jrelibplugin.jar;F:softwareJavajdk1.8.0_121jrelibresources.jar;F:softwareJavajdk1.8.0_121jrelibrt.jar;D:ideastudyspacespring_studyspring-studyspring-study-proxytargetclasses;E:softwareMavenRepositoryorgspringframeworkspring-webmvc3.2.0.RELEASEspring-webmvc-3.2.0.RELEASE.jar;E:softwareMavenRepositoryorgspringframeworkspring-context3.2.0.RELEASEspring-context-3.2.0.RELEASE.jar;E:softwareMavenRepositoryorgspringframeworkspring-aop3.2.0.RELEASEspring-aop-3.2.0.RELEASE.jar;E:softwareMavenRepositoryorgspringframeworkspring-core3.2.0.RELEASEspring-core-3.2.0.RELEASE.jar;E:softwareMavenRepositorycommons-loggingcommons-logging1.1.1commons-logging-1.1.1.jar;E:softwareMavenRepositoryorgspringframeworkspring-web3.2.0.RELEASEspring-web-3.2.0.RELEASE.jar;E:softwareMavenRepositoryaopallianceaopalliance1.0aopalliance-1.0.jar;E:softwareMavenRepositoryorgspringframeworkspring-expression3.2.0.RELEASEspring-expression-3.2.0.RELEASE.jar;E:softwareMavenRepositoryorgspringframeworkspring-beans3.2.0.RELEASEspring-beans-3.2.0.RELEASE.jar;E:softwareMavenRepositorycomalibabafastjson1.2.47fastjson-1.2.47.jar com.shiguang.service.DemotableSizeFor方法返回值:8Process finished with exit code 0此时我们设置初始容量cap=9
import java.lang.reflect.Method;public class Demo {public static void main(String[] args) throws Exception {Class aClass = Class.forName("java.util.HashMap");Object instance = aClass.newInstance();Method tableSizeFor = aClass.getDeclaredMethod("tableSizeFor", int.class);//设置目标类中的tableSizeFor方法可访问tableSizeFor.setAccessible(true);//将初始值时变更为cap=9Object invoke = tableSizeFor.invoke(instance, 9);//打印返回值System.out.println("tableSizeFor方法返回值:"+invoke);}}


推荐阅读