当前位置:网站首页>面试:hashCode()和equals()
面试:hashCode()和equals()
2022-06-12 16:05:00 【眠秋雨】
Q:hashCode()有什么用?
hashCode()是Object类中定义的一个本地方法,返回该对象的哈希值,用于确定该对象在哈希表中的索引位置。
Q:hashCode()怎么用?
当向哈希表中插入数据时,会先计算该对象的哈希值来确定插入位置,如果该位置是没有元素,那么直接插入成功。如果有元素,哈希表会利用equals()方法与该位置的元素进行比较。如果待插入的元素与其equals()返回true,那么插入失败,如果返回false,那么哈希表会将其散列到其它位置或者使用拉链法加入其中。
Q:为什么java要提供这两个方法呢?
因为有了hashcode(),在判断元素是否相等时效率更高。
Q:为什么重写equals()时要重写hashcode()?
保证equals()返回True时hashcode()也相等。如果不重写,可能会有equals()返回true,但是两对象的hashcode()不等。
边栏推荐
- Global and Chinese markets of automatic glue applicators 2022-2028: Research Report on technology, participants, trends, market size and share
- Divide training set, test set and verification set
- Analysis on the current situation of China's antiarrhythmic drug industry in 2021: domestic R & D is further [figure]
- 办公室VR黄片,骚操作!微软HoloLens之父辞职!
- Import and export steps of SQL Server 2008
- 5-5配置Mysql复制 基于日志点的复制
- Office VR porn, coquettish operation! The father of Microsoft hololens resigns!
- Decision tree classification and examples
- [weekly replay] game 80 of leetcode
- Escape rules and examples of go
猜你喜欢

Step by step steps to create an ABAP program with a custom screen

Remote control of other computers -- detailed tutorial
![Analysis of China's cargo transport volume, cargo transport turnover and port cargo in 2021 [figure]](/img/ee/ae9984355a98f5529dd4574eea2e7c.jpg)
Analysis of China's cargo transport volume, cargo transport turnover and port cargo in 2021 [figure]

FPGA (III) trigger and latch

Homology? Cross domain? How to solve cross domain problems?

Office VR porn, coquettish operation! The father of Microsoft hololens resigns!

Apache kylin Adventure
![Analysis of global and Chinese shipbuilding market in 2021: the global shipbuilding new orders reached 119.85 million dwt, with China, Japan and South Korea accounting for 96.58%[figure]](/img/3e/b54b7f15c4a6326d8c7c4433388a3a.jpg)
Analysis of global and Chinese shipbuilding market in 2021: the global shipbuilding new orders reached 119.85 million dwt, with China, Japan and South Korea accounting for 96.58%[figure]

Introduction and download website of common data of GIS, remote sensing, hydrology and Geography (2), supplementary~

Applet: how to get the user's mobile number in the plug-in
随机推荐
一步步创建包含自定义 Screen 的 ABAP 程序的详细步骤
Chapter I linear table
任务 输出密雪冰城主题曲 0612
Analysis of global and Chinese shipbuilding market in 2021: the global shipbuilding new orders reached 119.85 million dwt, with China, Japan and South Korea accounting for 96.58%[figure]
聊聊事件监听那些事-上
5g new scheme! Upgrade the existing base station and UE simulator to 5g millimeter wave band
PHP builds a high-performance API architecture based on sw-x framework (II)
Global and Chinese market for commercial ceiling fans 2022-2028: Research Report on technology, participants, trends, market size and share
Escape rules and examples of go
Use of packet capturing tool Fiddler: simulating speed limit test process in weak network environment
Tensorflow function: tf nn. in_ top_ k()
Explanation of socket principle (where, what and how to use it)
< 山东大学软件学院项目实训 > 渲染引擎系统——基础渲染器(三)
Solutions to some problems of scuacm22 retreat competition before summer training
Scanpy (VI) analysis and visualization of spatial transcriptome data
第一章 线性表
办公室VR黄片,骚操作!微软HoloLens之父辞职!
Project training of Software College of Shandong University rendering engine system basic renderer (6)
(四)GoogleNet复现
借助SpotBugs将程序错误扼杀在摇篮中