Hashed collections哈希集合
2024-10-14 11:40:30
【定义】
有index的集合
【hash的原理】
term for a situation when two different objects return the same hashcode: hash collision
就是无规律的一一对应排序,相同object对应的HASH应该相同,相同对应的HASH应该不同。具体实现:hashCode(It returns an int hash-code depending on the memory address of the object 根据obeject的位置来对应in the heap) equal() 都是自动继承的。
Use a function on hashcode, such as modulo, to calculate the index into the hashtable and then store the object at that index 根据hash的index来查找物体
【实现】
object有三种方法toString equal hashcode,通过传入memory address来计算值。同一obeject的不同state状态:要避免collision。
public int hashCode() {
return Objects.hash(title, author, year); //hash on instance variable values
}
//equals() implements equivalence relation - reflexive, symmetric, transitive, consistent –
// on non-null object references
@Override
public boolean equals(Object o) {
if (o == null) return false; // if o is null, then it can’t be equal to ‘this’
if (this == o) return true; // if both point to same object, then they are equal
if (getClass() != o.getClass()) return false; //if not of same type, they can’t be equal
Book b = (Book) o; //now that we know o is unique, non-null Book object, cast it to Book
return title.equals(b.title) && author.equals(b.author) && (year == b.year); //compare values
}
【步骤】
先用hashcode,再用equals。因为state不同要overload, 只overload一个会发生inconsistent。
【区别】
用object/key来hash
【treemap】
排序的map,里面是BST
【map的三个集合】
entryset keyset values
remove(k) 是remove key = k的entry
最新文章
- IDDD 实现领域驱动设计-SOA、REST 和六边形架构
- Ubuntu 部署 Node.js 应用
- HTML5滑动(swipe)事件
- ORACLE10g数据库字符集设置和客户端字符集设置不一致问题
- 05传智_jbpm与OA项目_部门模块中增加部门的jsp页面增加一个在线编辑器功能
- CMD和DOS的区别
- C语言回滚(二)--循环打印
- beej's 网络编程 打包数据pack data
- CloudStack 4.2 与CloudStack 4.1二级存储API发生变化
- 第48条:如果需要精确的答案,请避免使用float和double
- 开发专题指南: JEECG高速微云开发平台前言
- Powershell 执行外部命令
- baidu地图让多个标注出现在最佳视野
- python实现人人网用户数据爬取及简单分析
- 转:WebDriver(Selenium2) 处理可能存在的JS弹出框
- PHP连接和拆分数组array_combine()和array_slice()用法示例
- Lua内存分析工具
- Pandas 0 数据结构Series
- (python)数据结构---字典
- Java中volatile关键字解析
热门文章
- leetcode解题报告 32. Longest Valid Parentheses 动态规划DP解
- hadoop-2
- CSS DISPLAY AND POSITIONING
- World Cup 996B(排队模拟)
- 剑指offer例题——裴波那契数列
- Shell条件表达式
- 关于jsp基本语法:第一章节
- C++ 自定义控件的移植(将在其它程序中设计的自定义控件,移植到现在的系统中)
- 把Swift中的String转成NSString ,获取NSString的方法
- 用git bash 传数据到远程出错:git push origin master 出错:error: failed to push some refs to