简体   繁体   English

如何将字符串散列到RandomAccessFile

[英]How do I hash strings to RandomAccessFile

I'm having trouble understand how my teacher wants us to do the second part of this project. 我很难理解我的老师希望我们如何做这个项目的第二部分。 The first part was easy getting the employees and putting it in a raf, but I dont understand how i can hash ssn which is a string. 第一部分很容易得到员工并将其放入raf中,但是我不明白如何对ssn进行哈希处理。 Can someone please explain it. 有人可以解释一下吗? I put the directions just in case.Thanks 我把指示以防万一。谢谢

ssn: String(9 characters), fullname : string of 50 characters, salary: float, and age: int ssn:字符串(9个字符),全名:50个字符的字符串,salary:float,age:int

1.The program writes at least 20 Employees and stores them sequentially on the random access, and then sequentially reads and prints all Employees in a readable format. 1.该程序至少写入20名员工,并在随机访问时按顺序存储它们,然后以可读格式依次读取和打印所有员工。

2.Redo the previous part assuming the employees are stored based on hashing the ssn( non-sequentially) 2,重做上一部分,假设员工是基于对ssn进行散列存储的(非顺序)

You can create a hash of pretty much any type. 您可以创建几乎任何类型的哈希。 If I understand correctly, we want to use a distinct data value, an SSN, to make a hash that is used for keying into some random-access store. 如果我理解正确的话,我们想使用一个不同的数据值SSN来生成用于键入某些随机存取存储的哈希值。

My first thought is, if you need a strong hash based on more than just the String itself, to wrap the SSN in a Class, and use that to both store and fetch the SSN value, as well as provide a hashCode() implementation based on one or more fields along with a seed. 我的第一个想法是,如果您需要一个不仅仅基于String本身的强大哈希,请将SSN包装在Class中,并使用它来存储和获取SSN值,以及提供基于hashCode()的实现在一个或多个田地上,连同种子。 This is described a bit in Block's "Effective Java", where a HashCodeUtil class is introduced. 在Block的“ Effective Java”中对此进行了一些描述,其中引入了HashCodeUtil类。

A working example is discussed here: http://www.javapractices.com/topic/TopicAction.do?Id=28 这里讨论了一个工作示例: http : //www.javapractices.com/topic/TopicAction.do?Id=28

It looks to me like you are trying to implement your own hash table using RandomAccessFile. 在我看来,您正在尝试使用RandomAccessFile实现自己的哈希表。 If that is true what you need to do is take a hashcode and turn it into a slot number. 如果是这样,您需要执行一个哈希码并将其转换为插槽号。 To do this modulo divide the hashcode() of the SSN string by some prime number close to double the number of entries you expect in the table. 为此,以模数形式将SSN字符串的hashcode()除以某个质数,以使其接近表中您期望的条目数的两倍。 In your case maybe 41. Each slot in the RandomAccessFile is the size of an entry. 在您的情况下,可能是41。RandomAccessFile中的每个插槽都是一个条目的大小。 In you case 9+50+4+4 = 67. Multiply the slot number by the entry size to give you the offset of the location in the RandomAccessFile to read and write. 在您的情况下,9 + 50 + 4 + 4 =67。将插槽号乘以条目大小,即可获得RandomAccessFile中要读取和写入的位置的偏移量。

Note that this, as in all hash tables, may lead to collisions. 请注意,与所有哈希表一样,这可能导致冲突。 A real world implementation would then handle some kind of entry chaining. 然后,现实世界中的实现将处理某种入口链。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何刷新'RandomAccessFile'(java)? - How do I flush a 'RandomAccessFile' (java)? 如何从RandomAccessFile中写入和删除对象? - How do I write and delete Objects from a RandomAccessFile? 如何为大字符串制作一个好的 hash function? - How do I make a good hash function for large Strings? 如何附加到RandomAccessFile?每次运行此代码时,数据都会被覆盖 - How do I append to a RandomAccessFile? Every time I run this code, the data get overwritten 如何将InputStream用于RandomAccessFile的一部分? - How can I use an InputStream for parts of a RandomAccessFile? 我应该如何将POJO表示形式写入RandomAccessFile? - How should I write a POJO representation to a RandomAccessFile? 使用RandomAccessFile和多线程时,如何拆分大字符文件? - When using RandomAccessFile and Multi Thread, How do I split big char file? 使用java.io.RandomAccessFile,如何编写文件并始终将内容添加到开头? - Using java.io.RandomAccessFile, how do I write a file and keep adding content to the beginning? 能否以及如何将RandomAccessFile与FTP服务器上包含的文件一起使用? - Can and How do you use RandomAccessFile with a file contained on an FTP server? 如何使用Java中的RandomAccessFile打开现有目录? - How can I open an existing directory using RandomAccessFile in Java?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM