java学习笔记 集合 --- Set 哈希值
有勇气的牛排
1141
Java
2021-08-20 00:14:44
1 Set哈希值
哈希值:是JDK根据对象的地址
或者字符串
或者数字
算出来的int类型的数值
Object类中有一个方法可以获取对象哈希值
(1)public int hashCode
:返回对象的哈希码值
特点
(1)同一个对象多次调用hashCode()方法返回的哈希值时相同的
(2)默认情况下,不同对象的哈希值时不同的,而重写hashCode()方法,可以实现让不同对象的哈希值相同
package Hash;
public class HashDemo {
public static void main(String[] args) {
Student s1 = new Student("灰太狼", 20);
System.out.println(s1.hashCode());
System.out.println(s1.hashCode());
System.out.println("--------");
Student s2 = new Student("灰太狼", 20);
System.out.println(s2.hashCode());
System.out.println("--------");
System.out.println("hello".hashCode());
System.out.println("world".hashCode());
System.out.println("java".hashCode());
System.out.println("--------");
System.out.println("牛排".hashCode());
System.out.println("勇气".hashCode());
}
}
参考地址
https://www.bilibili.com/video/BV18J411W7cE?p=239
<h2><a id="1_Set_0"></a>1 Set哈希值</h2>
<p>哈希值:是JDK根据对象的<code>地址</code>或者<code>字符串</code>或者<code>数字</code>算出来的int类型的数值</p>
<p>Object类中有一个方法可以获取对象哈希值</p>
<p>(1)<code>public int hashCode</code>:返回对象的哈希码值</p>
<p><strong>特点</strong></p>
<p>(1)同一个对象多次调用hashCode()方法返回的哈希值时相同的</p>
<p>(2)默认情况下,不同对象的哈希值时不同的,而重写hashCode()方法,可以实现让不同对象的哈希值相同</p>
<pre><div class="hljs"><code class="lang-java"><span class="hljs-keyword">package</span> Hash;
<span class="hljs-keyword">public</span> <span class="hljs-keyword">class</span> <span class="hljs-title class_">HashDemo</span> {
<span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">main</span><span class="hljs-params">(String[] args)</span> {
<span class="hljs-comment">// 创建学生对象</span>
<span class="hljs-type">Student</span> <span class="hljs-variable">s1</span> <span class="hljs-operator">=</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">Student</span>(<span class="hljs-string">"灰太狼"</span>, <span class="hljs-number">20</span>);
<span class="hljs-comment">// 同一个对象多次调用 hashCode() 方法的哈希值时相同的</span>
System.out.println(s1.hashCode()); <span class="hljs-comment">// 460141958</span>
System.out.println(s1.hashCode()); <span class="hljs-comment">// 460141958</span>
System.out.println(<span class="hljs-string">"--------"</span>);
<span class="hljs-type">Student</span> <span class="hljs-variable">s2</span> <span class="hljs-operator">=</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">Student</span>(<span class="hljs-string">"灰太狼"</span>, <span class="hljs-number">20</span>);
<span class="hljs-comment">// 默认情况下,不同对象的哈希值时不同的</span>
<span class="hljs-comment">// 通过方法重写,可以实现不同对象的哈希值是相同的</span>
System.out.println(s2.hashCode()); <span class="hljs-comment">// 1163157884</span>
System.out.println(<span class="hljs-string">"--------"</span>);
System.out.println(<span class="hljs-string">"hello"</span>.hashCode()); <span class="hljs-comment">// 99162322</span>
System.out.println(<span class="hljs-string">"world"</span>.hashCode()); <span class="hljs-comment">// 113318802</span>
System.out.println(<span class="hljs-string">"java"</span>.hashCode()); <span class="hljs-comment">// 3254818</span>
System.out.println(<span class="hljs-string">"--------"</span>);
<span class="hljs-comment">// 这里有些版本是 不同汉字 相同哈希</span>
System.out.println(<span class="hljs-string">"牛排"</span>.hashCode()); <span class="hljs-comment">// 933015</span>
System.out.println(<span class="hljs-string">"勇气"</span>.hashCode()); <span class="hljs-comment">// 684589</span>
}
}
</code></div></pre>
<p>参考地址<br />
https://www.bilibili.com/video/BV18J411W7cE?p=239</p>
留言