HashMap的底层实现原理及JDK的优化

R语言 专栏收录该内容
¥59.90 ¥99.00
70篇文章

HashMap的底层实现原理及JDK的优化

HashMap是Java中常用的数据结构之一,它提供了一种快速的键值对存储和检索方式。在底层,HashMap通过一个数组加链表(或红黑树)的数据结构来实现。本文将详细介绍HashMap的底层实现原理,并讨论JDK对HashMap进行的优化。

HashMap底层实现原理:
HashMap内部使用一个数组来存储数据,每个数组元素称为桶(bucket)。当我们向HashMap中插入一个键值对时,通过哈希函数计算键的哈希码,并根据哈希码找到对应的桶。如果多个键的哈希码相同,它们将被放置在同一个桶中,形成一个链表。这样,具有相同哈希码的键值对可以存储在同一个桶中,实现快速的查找。

在Java 8之前,HashMap使用的是数组加链表的方式来解决哈希冲突。当链表长度超过一定阈值(默认为8)时,链表将被转换为红黑树,以提高查找效率。这样,HashMap在大多数情况下具有O(1)的查找复杂度。

JDK对HashMap的优化:
JDK在不同版本中对HashMap进行了一些优化,以提高性能和减少内存占用。

  1. 扩容机制的优化:
    在HashMap中,当元素数量超过容量的75%(默认加载因子)时,会触发扩容操作。扩容操作会创建一个更大的数组,并重新计算元素在新数组中的位置。在Java 8之前,扩容操作较为耗时,因为需要将元素重新散列到新数组中。但在Java 8及以后的版本中,采用了一种优化方式,称为“红黑树化”的优化。当发生扩容时,HashMap会检查链表长度是否超过阈值(默认为8),如果超过,则将链表转换为红黑树。这样可以减少元素重新散列的次数,提高性能。

  2. 红黑树的优化:
    在Jav

java基础篇--HashMap 底层实现原理是什么?JDK8 做了哪些优化? HashMap 是使用频率最高的类型之一,同时也是面试经常被问到的问题之一,这是因为 HashMap 的知识点有很多,同时它又属于 Java 基础知识的一部分,因此在面试中经常被问到。 本课时的面试题是,HashMap 底层是如何实现的?在 JDK 1.8 中它都做了哪些优化? 在 JDK 1.7 中 HashMap 是以数组加链表的形式组成的,JDK 1.8 之后新增了红黑树的组成结构,当链表大于 8 并且容量大于 64 时,链表结构会转换成红黑树结构,它的组成结构如下图所示: 数组中的元素我们. 阅读详情

相关推荐

嵌入式Linux网络调试:YT8531/YT8521 PHY驱动移植与设备树配置避坑指南

本文深入解析嵌入式Linux系统中YT8531/YT8521 PHY驱动移植与设备树配置的关键技术,涵盖内核驱动适配、RGMII接口时序调整及常见问题排查。针对Motorcomm千兆以太网PHY芯片,提供详细的设备树配置示例和调试技巧,帮助开发者规避典型设计陷阱,确保网络功能稳定运行。

weixin_42525632的博客 683

从零部署DeepSeek V4 Flash:NVIDIA DGX Spark实战指南

GPU加速计算已成为现代人工智能基础设施的核心,其原理是通过并行处理架构大幅提升深度学习模型的训练与推理效率。在AI工程实践中,NVIDIA GPU凭借CUDA生态和专用驱动栈,为大规模语言模型部署提供了硬件基础。本文聚焦于企业级AI服务器部署场景,针对NVIDIA DGX Spark多节点系统的硬件初始化、驱动兼容性配置、容器化部署等关键环节,提供从系统层到应用层的完整解决方案。通过vLLM推理框架的实战配置,演示如何将DeepSeek V4 Flash模型高效部署于多GPU环境,涵盖Tensor Par

weixin_32296621的博客 403

电流采样电路设计:差分放大与ADC采集详解

在电机控制、电源管理、电池监测等应用中,电流采样是一个关键环节。通常,我们使用小阻值的采样电阻将电流信号转换为电压信号,再通过运放放大后送入ADC进行采集。本文将详细分析一种基于差分放大器的电流采样电路,包括信号放大、参考电压偏置、滤波保护以及ADC转换计算等内容。

W0s477的博客 2万+

YOLOv8缺陷检测可视化界面实战:PyQt5布局设计与代码实现

本文详细介绍了如何利用PyQt5为YOLOv8缺陷检测模型构建一个专业、易用的可视化界面。通过实战讲解PyQt5的布局管理器设计、信号槽机制与YOLOv8核心的集成,帮助开发者快速将AI模型部署为直观的桌面应用,提升工业质检等场景的落地效率。

redis7keeper的博客 765

C语言关键字static的绝妙用途

为什么要说static妙,它确实是妙,在软件开发或者单片机开发过程中,大家总以为static就是一个静态变量,在变量类型的前面加上就自动清0了,还有就是加上static关键字的,不管是变量还是关键字,在局部变量中,变量仅限于局部可见,在全局区中,static变量或函数仅限于本文件可见,当然还有一个重要的特性被忽略了。。。。       就是static修饰的局部变量 它的数值 是上一次函数调用结

Bruce.yang的嵌入式之旅 5383

使用ESP32设计嵌入式蓝牙游戏手柄

通过使用ESP32开发板和相关的库,我们可以设计和实现嵌入式蓝牙游戏手柄。连接到该设备后,您可以使用控制器模块的按钮和摇杆等元件来模拟游戏手柄的输入。根据您使用的具体模块和引脚分配,连接按钮和摇杆等控制元件到ESP32的GPIO引脚。ESP32是一款功能强大的微控制器,集成了Wi-Fi和蓝牙功能,非常适合用于开发物联网和嵌入式应用。请注意,本文仅提供了一个基于ESP32的嵌入式蓝牙游戏手柄设计示例,具体实现可能因硬件和需求而有所不同。确保开发板正确连接到计算机,并选择正确的开发板和端口。

CodeMaven的博客 1645

USB 基础学习:USB 电气信号介绍

USB电气信号技术解析 USB通信的核心是差分信号传输技术,通过D+和D-两根线传输相位相反的电压信号实现数据通信。USB信号系统包含三类关键信号:差分数据信号(抗干扰性强)、单端状态信号(用于设备识别)和电源信号(提供5V供电)。USB2.0采用400mV电压摆幅和NRZI编码,支持低速(1.5Mbps)至高速(480Mbps)传输;USB3.0则新增专用差分对,数据速率可达20Gbps。物理层通过上拉/下拉电阻检测设备连接,高速模式需特殊握手协议。抗干扰设计的关键在于保持差分线等长、阻抗匹配和使用共模扼

浩瀚之水的专栏 1790

手把手教你学pcie--性能调优 - 中断机制优化

在PCIe系统开发中,中断机制是主机与设备之间通信的重要组成部分。然而,不当的中断设计可能导致CPU负载过高、延迟增加等问题。本文将详细介绍如何优化PCIe系统的中断机制,提升系统性能和响应速度。传统中断(INTx):通过专用引脚发送中断信号,可能引发冲突且效率较低。消息中断(MSI):通过内存写操作发送中断消息,避免冲突并提高效率。扩展消息中断(MSI-X):基于MSI改进,支持更多独立中断向量,适用于复杂场景。选择合适的中断模式对系统性能至关重要。

MHD0815的博客 309

深度学习工具包 DeepLearningToolbox

深度学习工具包Deprecation notice.-----This toolbox is outdated and no longer maintained.There are much better tools available for deep learning than this toolbox, e.g. [Theano](http://deeplearning.net/software/theano/), [torch](http://torch.ch/) or [tensorflow](http://www.tensorflow.org/)I would suggest you use one of the tools mentioned above rather than use this toolbox.Best, Rasmus.DeepLearnToolbox================A Matlab toolbox for Deep Learning.Deep Learning is a new subfield of machine learning that focuses on learning deep hierarchical models of data.It is inspired by the human brain's apparent deep (layered, hierarchical) architecture.A good overview of the theory of Deep Learning theory is[Learning Deep Architectures for AI](http://www.iro.umontreal.ca/~bengioy/papers/ftml_book.pdf)For a more informal introduction, see the following videos by Geoffrey Hinton and Andrew Ng.* [The Next Generation of Neural Networks](http://www.youtube.com/watch?v=AyzOUbkUf3M) (Hinton, 2007)* [Recent Developments in Deep Learning](http://www.youtube.com/watch?v=VdIURAu1-aU) (Hinton, 2010)* [Unsupervised Feature Learning and Deep Learning](http://www.youtube.com/watch?v=ZmNOAtZIgIk) (Ng, 2011)If you use this toolbox in your research please cite [Prediction as a candidate for learning deep hierarchical models of data](http://www2.imm.dtu.dk/pubdb/views/publication_details.php?id=6284)```@MASTERSTHESIS\{IMM2012-06284, author = "R. B. Palm", title = "Prediction as a candidate for learning deep hierarchical models of data", year = "2012",}```Contact: rasmusbergpalm at gmail dot comDirectories included in the toolbox-----------------------------------`NN/` - A library for Feedforward Backpropagation Neural Networks`CNN/` - A library for Convolutional Neural Networks`DBN/` - A library for Deep Belief Networks`SAE/` - A library for Stacked Auto-Encoders`CAE/` - A library for Convolutional Auto-Encoders`util/` - Utility functions used by the libraries`data/` - Data used by the examples`tests/` - unit tests to verify toolbox is workingFor references on each library check REFS.mdSetup-----1. Download.2. addpath(genpath('DeepLearnToolbox'));Example: Deep Belief Network---------------------```matlabfunction test_example_DBNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit RBM and visualize its weightsrand('state',0)dbn.sizes = [100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);figure; visualize(dbn.rbm{1}.W'); % Visualize the RBM weights%% ex2 train a 100-100 hidden unit DBN and use its weights to initialize a NNrand('state',0)%train dbndbn.sizes = [100 100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);%unfold dbn to nnnn = dbnunfoldtonn(dbn, 10);nn.activation_function = 'sigm';%train nnopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.10, 'Too big error');```Example: Stacked Auto-Encoders---------------------```matlabfunction test_example_SAEload mnist_uint8;train_x = double(train_x)/255;test_x = double(test_x)/255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit SDAE and use it to initialize a FFNN% Setup and train a stacked denoising autoencoder (SDAE)rand('state',0)sae = saesetup([784 100]);sae.ae{1}.activation_function = 'sigm';sae.ae{1}.learningRate = 1;sae.ae{1}.inputZeroMaskedFraction = 0.5;opts.numepochs = 1;opts.batchsize = 100;sae = saetrain(sae, train_x, opts);visualize(sae.ae{1}.W{1}(:,2:end)')% Use the SDAE to initialize a FFNNnn = nnsetup([784 100 10]);nn.activation_function = 'sigm';nn.learningRate = 1;nn.W{1} = sae.ae{1}.W{1};% Train the FFNNopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.16, 'Too big error');```Example: Convolutional Neural Nets---------------------```matlabfunction test_example_CNNload mnist_uint8;train_x = double(reshape(train_x',28,28,60000))/255;test_x = double(reshape(test_x',28,28,10000))/255;train_y = double(train_y');test_y = double(test_y');%% ex1 Train a 6c-2s-12c-2s Convolutional neural network %will run 1 epoch in about 200 second and get around 11% error. %With 100 epochs you'll get around 1.2% errorrand('state',0)cnn.layers = { struct('type', 'i') %input layer struct('type', 'c', 'outputmaps', 6, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %sub sampling layer struct('type', 'c', 'outputmaps', 12, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %subsampling layer};cnn = cnnsetup(cnn, train_x, train_y);opts.alpha = 1;opts.batchsize = 50;opts.numepochs = 1;cnn = cnntrain(cnn, train_x, train_y, opts);[er, bad] = cnntest(cnn, test_x, test_y);%plot mean squared errorfigure; plot(cnn.rL);assert(er<0.12, 'Too big error');```Example: Neural Networks---------------------```matlabfunction test_example_NNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);% normalize[train_x, mu, sigma] = zscore(train_x);test_x = normalize(test_x, mu, sigma);%% ex1 vanilla neural netrand('state',0)nn = nnsetup([784 100 10]);opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samples[nn, L] = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.08, 'Too big error');%% ex2 neural net with L2 weight decayrand('state',0)nn = nnsetup([784 100 10]);nn.weightPenaltyL2 = 1e-4; % L2 weight decayopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex3 neural net with dropoutrand('state',0)nn = nnsetup([784 100 10]);nn.dropoutFraction = 0.5; % Dropout fraction opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex4 neural net with sigmoid activation functionrand('state',0)nn = nnsetup([784 100 10]);nn.activation_function = 'sigm'; % Sigmoid activation functionnn.learningRate = 1; % Sigm require a lower learning rateopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex5 plotting functionalityrand('state',0)nn = nnsetup([784 20 10]);opts.numepochs = 5; % Number of full sweeps through datann.output = 'softmax'; % use softmax outputopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex6 neural net with sigmoid activation and plotting of validation and training error% split training data into training and validation datavx = train_x(1:10000,:);tx = train_x(10001:end,:);vy = train_y(1:10000,:);ty = train_y(10001:end,:);rand('state',0)nn = nnsetup([784 20 10]); nn.output = 'softmax'; % use softmax outputopts.numepochs = 5; % Number of full sweeps through dataopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, tx, ty, opts, vx, vy); % nntrain takes validation set as last two arguments (optionally)[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');```[![Bitdeli Badge](https://d2weczhvl823v0.cloudfront.net/rasmusbergpalm/deeplearntoolbox/trend.png)](https://bitdeli.com/free "Bitdeli Badge")

LinePhone3.2.7(android端最新版本) sip客户端

SIP客户端For android:LinePhone3.2.7(最新版本),官网地址:http://www.linphone.org/

Data-Structures-and-Algorithm-Analysis-in-Java.pdf

作者: Weiss, Mark A. 出版社: Addison Wesley Data Structures and Algorithm Analysis in Java is an “advanced algorithms” book that fits between traditional CS2 and Algorithms Analysis courses. In the old ACM Curriculum Guidelines, this course was known as CS7. This text is for readers who want to learn good programming and algorithm analysis skills simultaneously so that they can develop such programs with the maximum amo...

汉书HE8,好用的汇编代码编写工具

汉书HE8,好用的汇编代码编写工具早期编写汇编代码所用功能强大

上一篇: 计算滑动分组的中位数并将生成的统计数据合并到原数据集中(使用R语言)
下一篇: 使用 ggsci 包中的 `scale_color_uchicago` 函数指定可视化图像的配色符合芝加哥大学配色要求(R语言)
普通网友
博客等级 码龄3年 225粉丝 642原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符  | 博主筛选后可见
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值
⌂ 换网址