HW 3: Hashing| CS 61B Data Structures, Spring 2019

CS61B Homework5作业回顾(附代码) HW5一开始做不出来的同学可以继续先看Lecture,Encapsulated List那一节老师会回顾一下HW5,对作业有启发思路的作用。Part1.HW5的DListNode增加一个myList的field,用于确认node位于哪个list,同时也为后面的isValidNode()服务。remove()之后需要将myList, prev, next 统统指向null,不然这个node的prev... 阅读详情

equals

As mentioned in class, writing a proper equals method is a little tricker than it might sound at first blush. According to the Java language specification, your equals method should have the following properties to be in compliance:

  • Reflexive: x.equals(x) must be true for any non-null x.
  • Symmetric: x.equals(y) must be the same as y.equals(x) for any non-null x and y.
  • Transitive: if x.equals(y) and y.equals(z), then x.equals(z) for any non-null x, y, and z.
  • Consistent: x.equals(y) must return the same result if called multiple times, so long as the object referenced by x and y do not change.
  • Not-equal-to-null: x.equals(null) should be false for any non-null x.

One particularly vexing issue is that the argument passed to the equals method is of type Object, not of type SimpleOomage, so you will need to do a cast. However, doing a cast without verifying that the Object is a SimpleOomage won’t work, because you don’t want your code to crash if someone calls .equals with an argument that is not a SimpleOomage. Thus, we’ll need to use a new method of the Object class called getClass. For an example of a correct implementation of equals, see

 /**
     * Compares this date to the specified date.
     *
     * @param  other the other date
     * @return {@code true} if this date equals {@code other}; {@code false} otherwise
     */
    @Override
    public boolean equals(Object other) {
        if (other == this) return true;
        if (other == null) return false;
        if (other.getClass() != this.getClass()) return false;
        Date that = (Date) other;
        return (this.month == that.month) && (this.day == that.day) && (this.year == that.year);
    }
  1. A Simple hashCode
    In Java, it is critically important that if you override equals that you also override hashCode.

To understand more deeply why this failure occurs, consider the code shown below.

Two questions to ponder when reading this code:

  • What should each print statement output?
  • What will each print statement output?
public void testHashCodeAndEqualsConsistency() {
     SimpleOomage ooA = new SimpleOomage(5, 10, 20);
     SimpleOomage ooA2 = new SimpleOomage(5, 10, 20);

     System.out.println(ooA.equals(ooA2));

     HashSet<SimpleOomage> hashSet = new HashSet<SimpleOomage>();
     hashSet.add(ooA);
     System.out.println(hashSet.contains(ooA2));
 }

Answers:

  • The first print statement should and will output true, according to the definition of equals that we created in the previous part of the assignment.
  • The final print statement should output true. The HashSet does contain a SimpleOomage with r/g/b values of 5/10/20!
  • The final print statement will print false. When the HashSet checks to see if ooA2 is there, it will first compute ooA2.hashCode, which for our code will be the default hashCode(), which is just the memory address. Since ooA and ooA2 have different addresses, their hashCodes will be different, and thus the HashSet will be unable to find an Oomage with r/g/b value of 5/10/20 in that bucket.

The Java specification for equals mentions this danger as well: “Note that it is generally necessary to override the hashCode method whenever the equals method is overridden, so as to maintain the general contract for the hashCode method, which states that equal objects must have equal hash codes.”

Note that this hashCode is now consistent with equals, so you should now pass all of the TestSimpleOomage tests.

3.计算hash code 时出现负值

You must convert from hashCode to bucket number using (hashCode & 0x7FFFFFFF) % M or Math.floorMod You should not use Math.abs(hashCode) % M.

Berkeley CS61B Spring 2019 打卡(1) 仅供自勉和记录所用刷题记录在Dropbox Paper上2019 May 20 刷题记录在Dropbox Paper上 2019 May 20 阅读详情

相关推荐

Linux 内核 下载 编译 安装 2021 ubuntu

Linux 内核 下载 编译 安装 ubuntu 2021 摘要 为什么要进行 调试 安装自定义的内核 Linux 内核 下载 Linux 内核 解压 linux 内核 配置 linux 内核编译 linux 内核 安装 1. 为什么要进行 调试 安装自定义的内核 对 内核 进行开发 去除 内核中 无用的模块 学习 如何 对内核进行 安装 2.下载内核 推荐下载地址 使用 git clone 命令 下载最新的 linux 内核 到本地 git clone git://git.kerne

yaoxinJJJ的博客 4131

CS61B - HW3 - Hashing - 溢出问题

HW3主要做了两件事情:重写equals和hashCode方法。有两个值得注意的点。 equals方法需要满足的条件 所以在重写equals方法时,必须额外考虑null, 其它class这两种特殊情况。 @Override public boolean equals(Object o) { if (o == this) return true; if (o...

fourier_transformer的博客 737

Linux 驱动的第一个程序:hello world | 内核编程 part 1

Linux 驱动的第一个程序:hello world | 内核编程 part 1 1.内容摘要 前提条件 hello world程序 如何 编译 一个 modul 加载和卸载一 个 模块 2.前提条件 2.1 Kernel headers 当前内核的内核头文件:通常,除非您已编译自己定制的内核,否则您将无法在系统上调用该 头文件 2.1.1获得头文件的两种方式 编译 并安装 自己定制的内核:参考我上一篇帖子 输入以下命令从 网上下载 # Arch, my fav :) $ pacman -S lin

yaoxinJJJ的博客 936

cs61b实验记录(五)HW3、Lab10

cs61b实验记录(五)HW3、Lab10 HW3 equals @Override public boolean equals(Object o) { if (o==this)return true; if (o==null)return false; if (o instanceof SimpleOomage){ SimpleOomage anothero=(SimpleOomage)o; retur

qq_45698833的博客 1003

LPDDR4引脚功能详解:硬件新人必看的设计与调试指南

内存接口是硬件设计中的核心难点,尤其是LPDDR4这类高速动态随机存储技术,引脚繁多且功能复杂。理解其引脚分组逻辑,是进行可靠硬件设计的基础。LPDDR4引脚可划分为时钟使能、命令地址、数据选通、阻抗校准与电源等几大类,其中ZQ引脚外接精密电阻用于校准驱动强度,直接影响信号完整性;CK差分时钟与CKE使能协同控制读写节拍;DQ与DQS构成字节通道。掌握这些引脚的功能与PCB布局约束,能帮助工程师快速定位training失败等问题。本文从工程实践角度,系统梳理LPDDR4引脚定义与调试经验,为硬件新人提供一份

weixin_29191761的博客 257

TTL与RS-232电平切换电路设计详解

在单片机、嵌入式及工业控制领域,串口通信是设备交互的基础,但TTL电平与RS-232电平在逻辑定义、电压幅度和传输距离上差异显著,直接互连极易导致通信失败。电平转换和通道切换是解决这类接口不匹配的关键技术。本文从两种电平标准的本质区别入手,解析以MAX232为代表的电荷泵升压/反压原理,介绍跳线帽、拨码开关及模拟开关等多种切换方案的设计要点,并给出完整的电路连接、参数计算、保护设计和波形测试方法。该电路适用于开发板调试、新旧设备兼容、工控模块一板多用等场景,能大幅提升硬件调试效率。围绕TTL/232切换,还

weixin_34216614的博客 297

CS 61A Spring 2019 HW02 学习笔记

CS 61A: Structure and Interpretation of Computer Programs 课程官网 Homework 2: Higher Order Functions 作业链接 参考答案 主要内容 函数名称、函数调用; 运算符** //; 纯函数与非纯函数。 可能用到的函数 from operator import add, mul, sub square =...

tac-kit的博客 4485

《YOLOv8实例分割实战:训练自己的数据集 》(视频课程)

本课程将手把手地教大家使用labelme标注和使用YOLOv8训练自己的数据集,完成一个多目标实例分割实战项目。

bai666ai的博客 792

2019CS61B的HW3 Lab8

写的代码都在这里面 HomeWork3 这个作业主要是让我们学习如何hash化。我这里就将我修改的部分粘贴过来,原文有的我就不放这里了。 对hash化而言,最主要是如何将对象转化成int类型,方便储存和寻址。 这里是 通过和类似于进制的思路 因为一共有255/5 = 51 51+1=52 因此将其当成3位52进制 进行计算 @Override public class SimpleOomage implements Oomage { public boolean equals(Obj

paleatta的博客 431

PyMOL下载安装教程(附安装包)2025最新版(PyMOL 3.0.3)

本文详细介绍了Windows系统下PyMOL分子建模软件的安装教程。主要内容包括:提供安装包下载链接,强调需关闭杀毒软件;分步骤讲解管理员身份运行安装程序、接受协议、选择安装路径等安装流程;重点说明如何配置隐藏文件夹ProgramData中的许可证文件;最后提供基础使用技巧和常见问题解决方案。该教程适用于需要制作高质量分子结构图的科研人员和新手用户,帮助完成软件部署并顺利开展工作。

progalchemist的博客 3899

保姆级教程:在Ubuntu 22.04上,用Chroot和xorriso定制你的专属服务器镜像(附离线包集成)

本文提供了一份详细的Ubuntu 22.04离线镜像定制教程,通过Chroot环境和xorriso工具,帮助用户打造专属服务器镜像。教程涵盖环境准备、镜像解构、系统定制、镜像重组与验证等全流程,特别适合需要在隔离网络环境中批量部署服务器的场景。

weixin_28704565的博客 460

【CS 61B】Data Structures, Spring 2021 -- Week 1(1. Intro and 2. Classes, Lists, Arrays, Maps)

【CS 61B】Data Structures, Spring 2021 -- Week 1(1. Intro and 2. Classes, Lists, Arrays, Maps)

汤姆lazy的CSDN博客 2870

将任意文件转换为Pcap网络数据包传输

一款实用的工具用于将任意文件转换为pcap网络数据包

村中少年的专栏 3196

CS61BHomework3作业回顾(附代码)

Part 1.smoosh()函数,目的是将数组中出现的所有数字产生重复的部分删除,例如11223334444经过smoosh函数后会变成1234. public static void smoosh(int[] ints) { // Fill in your solution here. (Ours is twelve lines long, not counting ...

everest115的博客 1063

【大语言模型 21】语言模型训练目标:MLM vs CLM vs GLM对比分析

本文深入对比分析三种主流语言模型训练目标:掩码语言模型(MLM)、因果语言模型(CLM)和双空白填充(GLM)。通过数学推导、代码实现和实验对比,揭示不同训练目标的工作机制、适用场景和性能差异,为选择合适的预训练策略提供科学指导。

熵数实验室 1183

Cadence PSpice 仿真11:电感型低通滤波器最坏情况分析仿真实战图文教程

在进行蒙特卡罗分析时会将元器件的容差考虑进去,然后通过运行多次的随机仿真,得到电路输出的一个范围。但是多数情况下设计师关注的只是电路的最坏情况是否满足设计要求。PSpice的最坏情况分析是取在容差的边界点元件参数进行仿真以计算出电路输出的最坏情况。所以如果如果电路中有10个元件的话,需要先取10个元件的标准值仿真1次,然后再分别在10个元件的容差边界处各仿真1次,然后再对仿真结果进行统计和计算,再最坏情况下再仿真1次。

fydar的博客 1953

CS61B(18Spring) - Project 2 - random world generator

Project 2 - random world generator项目要求 + 成品skeleton文档分析WorldGeneratorRoom.javaHallway迷宫生成算法连接Room和迷宫去除deadends去掉多余的WALL一些小细节总结 昨天晚上做完了phase 1,今天整理一下思路。为了节省时间,phase 2的交互部分就不做了感觉意义并不大。其实project 2难点就是在于随...

fourier_transformer的博客 3447

STM32F407中文手册

STM32f407中文手册 本参考手册面向应用开发人员,提供有关使用 STM32F405xx/07xx、STM32F415xx/17xx、 STM32F42xxx 和 STM32F43xxx 微控制器存储器与外设的完整信息。STM32F405xx/07xx、STM32F415xx/17xx、STM32F42xxx 和 STM32F43xxx 构成一个微 控制器系列,各产品具有不同的存储器大小、封装和外设。有关订购信息以及器件的机械与电气特性,请参见数据手册。有关 ARM Cortex™-M4F 内核的信息,请参见《Cortex™-M4F 技术参考手册》。

QEMU 安装与使用

这里写自定义目录标题下载并安装qemu 下载并安装qemu 下载 git clone https://gitlab.com/qemu-project/qemu.git 安装 #!/bin/bash rm -rf build mkdir build cd build sudo apt-get install build-essential zlib1g-dev pkg-config libglib2.0-dev binutils-dev libboost-all-dev autoconf

yaoxinJJJ的博客 5524

Devexpress16.2.6破解工具

Devexpress16.2.6破解工具Devexpress16.2.6破解工具Devexpress16.2.6破解工具Devexpress16.2.6破解工具

OV9734 DATASHEET,完整规格书

OV9734完整规格书,包括电气参数,尺寸规格,寄存器配置表等

如何设置 proxy

Git proxy [http] proxy = http://10.206.133.47:3128 [user] email = name@email.com name = name APT PROXY In file /etc/apt/apt.conf Acquire::http::proxy "http://10.206.133.47:3128/"; Acquire::https::proxy "http://10.206.133.47:3128/"; Acquire::http::No

yaoxinJJJ的博客 3293

迈瑞BS系列全自动生化分析仪LIS协议接口手册(内含HL7和ASTM)

迈瑞BS系列全自动生化分析仪LIS协议接口手册,内含HL7和ASTM)

如何 在 linux | ubuntu 安装 使用 更新 visual studio code

摘要 如何安装 visual studio code 如何 在命令行中 打开 vsc 如何安装 安装所需要的 依赖关系 sudo apt update sudo apt install software-properties-common apt-transport-https wget 引入Microsoft GPG key wget -q https://packages.microsoft.com/keys/microsoft.asc -O- | sudo apt-key add -

yaoxinJJJ的博客 1973

SPDK 入门

SPDK installation and Hardware check Compile SPDK $ cd spdk_repo/spdk $ git submodule update --init $ ./configure --enable-debug --enable-lto --with-shared $ make Verify we have an NVMe device $ lspci | grep "Non-Volatile" 00:0e.0 Non-Vola

yaoxinJJJ的博客 1912

如何 编译 uswap

1. 编译 uswap 下载 qemu 和 uswap的代码 git clone url/to/code 修改qemu 的 configure - slirp="" + slirp="no" -capstone="" +capstone="no" 在 qemu directory下运行命令下载需要的包 git submodule update --init --recursive 运行shell进行编译 ./build-uswap-and-qemu.sh 编译qemu (in q

yaoxinJJJ的博客 1006

clab8 总结|CS 61B Data Structures, Spring 2019

实战总结 Lab 8: HashMap /****************************************************************************** * Compilation: javac SeparateChainingHashST.java * Execution: java SeparateChainingHashST &l...

yaoxinJJJ的博客 639

Qemu disk pass through

qemu disk pass through

yaoxinJJJ的博客 375

Wildcard (Java)

wiki A bounded wildcard is one with either an upper or a lower inheritance constraint. The bounds can be both class and interface types. Upper bounds are expressed using the extends keyword and lower ...

yaoxinJJJ的博客 349
上一篇: clab8 总结|CS 61B Data Structures, Spring 2019
下一篇: Wildcard (Java)
yaoxinJJJ
博客等级 码龄7年 5粉丝 35原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值