hashcode

Java总结篇系列:java.lang.Object

扶醉桌前 提交于 2019-12-18 18:35:51
从本篇开始,将对Java中各知识点进行一次具体总结,以便对以往的Java知识进行一次回顾,同时在总结的过程中加深对Java的理解。 Java作为一个庞大的知识体系,涉及到的知识点繁多,本文将从Java中最基本的类java.lang.Object开始谈起。 Object类是Java中其他所有类的祖先,没有Object类Java面向对象无从谈起。作为其他所有类的基类,Object具有哪些属性和行为,是Java语言设计背后的思维体现。 Object类位于java.lang包中,java.lang包包含着Java最基础和核心的类,在编译时会自动导入。Object类没有定义属性,一共有13个方法,具体的类定义结构如下图: 1.类构造器public Object(); 大部分情况下 ,Java中通过形如 new A(args..)形式创建一个属于该类型的对象。其中A即是类名,A(args..)即此类定义中相对应的构造函数。通过此种形式创建的对象都是通过类中的构造函数完成。为体现此特性,Java中规定:在类定义过程中,对于未定义构造函数的类,默认会有一个无参数的构造函数,作为所有类的基类,Object类自然要反映出此特性,在源码中,未给出Object类构造函数定义,但实际上,此构造函数是存在的。 当然,并不是所有的类都是通过此种方式去构建,也自然的, 并不是所有的类构造函数都是public。 2

Hibernate: When is it necessary to implement equals() and hashCode(), and if so, how?

让人想犯罪 __ 提交于 2019-12-18 16:46:24
问题 Based on various bad experiences my rule of thumb as a Java programmer is to only implement equals() and hashCode() on immutable objects, where two instances of the object really are interchangeable. Basically I want to avoid situations like the HashMap key problem in that link, or like the following: Get a thing with a certain identity. Modify it. Add it to a set. (later) Get another thing with the same identity. Modify it. Add it to the same set. Fail to notice that this add doesn't

What happens if two different objects have the same hashcode?

冷暖自知 提交于 2019-12-18 11:47:42
问题 It is my understanding that two unequal objects can have the same hashcode. How would this be handled when adding or retrieving from a HashMap java? 回答1: They will just be added to the same bucket and equals() will be used to distinguish them. Each bucket can contain a list of objects with the same hash code. In theory you can return the same integer as a hash code for any object of given class, but that would mean that you loose all performance benefits of the hash map and, in effect, will

Why is a SHA-1 Hash 40 characters long if it is only 160 bit?

独自空忆成欢 提交于 2019-12-18 10:14:15
问题 The title of the question says it all. I have been researching SHA-1 and most places I see it being 40 Hex Characters long which to me is 640bit. Could it not be represented just as well with only 10 hex characters 160bit = 20byte. And one hex character can represent 2 byte right? Why is it twice as long as it needs to be? What am I missing in my understanding. And couldn't an SHA-1 be even just 5 or less characters if using Base32 or Base36 ? 回答1: One hex character can only represent 16

Why are hashCode() and getClass() native methods?

我怕爱的太早我们不能终老 提交于 2019-12-18 10:06:03
问题 I checked the source code of Object class where I found that method declaration of getClass() was public final native Class<?> getClass(); And the declaration of hashCode() was public native int hashCode(); Why are these two methods native methods in the class and how can I get the source code of those methods? 回答1: You can find the complete source code of the native methods here I hope this will work for you. These are native methods, because it has to interact with the machine. Here machine

Java基础教程——System类

一笑奈何 提交于 2019-12-18 09:27:13
System类 java.lang.System类代表当前Java程序的运行平台。 |-可以做输入输出,垃圾回收;(此处不讲) |-可以获取时间; |-可以获取环境变量; |-可以获取系统信息; |-可以获取对象的原始HashCode。(比如String类就改写了hashCode方法,不能唯一地标识一个对象) 获取时间 public class System1时间 { public static void main(String[] args) { System.out.println("--currentTimeMillis():UTC(世界标准时间) 1970.1.1开始到现在的时间差"); System.out.println("毫秒:" + System.currentTimeMillis()); System.out.println("nanoTime()只能用于测量已过的时间"); System.out.println("纳秒:" + System.nanoTime()); } } System.currentTimeMillis()可以用户计算一段代码运行所消耗的时间。 System.nanoTime()精确度太高,硬件环境不一定能精确到纳秒,因此这个方法并不常用。 获取环境变量 import java.util.Map; public class

Get a file SHA256 Hash code and Checksum

 ̄綄美尐妖づ 提交于 2019-12-18 08:20:32
问题 Previously I asked a question about combining SHA1+MD5 but after that I understand calculating SHA1 and then MD5 of a lagrge file is not that faster than SHA256. In my case a 4.6 GB file takes about 10 mins with the default implementation SHA256 with (C# MONO) in a Linux system. public static string GetChecksum(string file) { using (FileStream stream = File.OpenRead(file)) { var sha = new SHA256Managed(); byte[] checksum = sha.ComputeHash(stream); return BitConverter.ToString(checksum)

Should I use a concatenation of my string fields as a hash code?

旧时模样 提交于 2019-12-18 04:35:19
问题 I have an Address class in C# that looks like this: public class Address { public string StreetAddress { get; set; } public string RuralRoute { get; set; } public string City { get; set; } public string Province { get; set; } public string Country { get; set; } public string PostalCode { get; set; } } I'm implementing equality and so I need to override the hash code. At first I was going to use the hashcode formula from EJ but then I thought: These are all string fields, can't I just just use

Using a larger prime as a multiplier when overriding hashCode()

狂风中的少年 提交于 2019-12-18 03:22:30
问题 I have been reading about hashcode functions for the past couple of hours and have accumulated a couple of questions regarding use of prime numbers as multipliers in custom hashcode implementations. I would be grateful if I could get some insight regarding following questions: In a comment to @mattb's answer here, @hstoerr advocates for use of larger primes (such as 524287) instead of the common prime 31. My question is, given the following implementation of a hashcode functions for a pair or

Using a larger prime as a multiplier when overriding hashCode()

China☆狼群 提交于 2019-12-18 03:22:08
问题 I have been reading about hashcode functions for the past couple of hours and have accumulated a couple of questions regarding use of prime numbers as multipliers in custom hashcode implementations. I would be grateful if I could get some insight regarding following questions: In a comment to @mattb's answer here, @hstoerr advocates for use of larger primes (such as 524287) instead of the common prime 31. My question is, given the following implementation of a hashcode functions for a pair or