1. 程式人生 > >Java中的HashCode 1 之hash演算法基本原理

Java中的HashCode 1 之hash演算法基本原理

分享一下我老師大神的人工智慧教程!零基礎,通俗易懂!http://blog.csdn.net/jiangjunshow

也歡迎大家轉載本篇文章。分享知識,造福人民,實現我們中華民族偉大復興!

               

 

一、為什麼要有Hash演算法

Java中的集合有兩類,一類是List,一類是Set。List內的元素是有序的,元素可以重複。Set元素無序,但元素不可重複。要想保證元素不重複,兩個元素是否重複應該依據什麼來判斷呢?用Object.equals方法。但若每增加一個元素就檢查一次,那麼當元素很多時,後新增到集合中的元素比較的次數就非常多了。也就是說若集合中已有1000個元素,那麼第1001個元素加入集合時,它就要呼叫1000次equals方法。這顯然會大大降低效率。於是Java採用了雜湊表的原理。雜湊(Hash)是個人名,由於他提出雜湊演算法的概念就以他的名字命名了。

 

 

二、Hash演算法原理

當Set接收一個元素時根據該物件的記憶體地址算出hashCode,看它屬於哪一個區間,在這個區間裡呼叫equeals方法。

 

確實提高了效率。但一個面臨問題:若兩個物件equals相等,但不在一個區間,根本沒有機會進行比較,會被認為是不同的物件。所以Java對於eqauls方法和hashCode方法是這樣規定的:

1 如果兩個物件相同,那麼它們的hashCode值一定要相同。也告訴我們重寫equals方法,一定要重寫hashCode方法。

2 如果兩個物件的hashCode相同,它們並不一定相同,這裡的物件相同指的是用eqauls方法比較。

 

 

三、例子

 

1 沒有重寫hashCode和equals的方法 

package cn.xy.test;

public class Point1
{
 private int x;
 private int y;

 public Point1(int x, int y)
 {
  super();
  this.x = x;
  this.y = y;
 }

 public int getX()
 {
  return x;
 }

 public void setX(int x)
 {
  this.x = x;
 }

 public int getY()
 {
  return y;
 }

 public void setY(int y)
 {
  this.y = y;
 }

}

public class HashSetAndHashCode
{
 public static void main(String[] args)
 {
  HashSet<Point1> hs1 = new HashSet<Point1>();
  Point1 p11 = new Point1(3, 3);
  Point1 p12 = new Point1(3, 3);
  Point1 p13 = new Point1(3, 5);
  hs1.add(p11);
  hs1.add(p11);
  hs1.add(p12);
  hs1.add(p13);
  System.out.println(hs1.size());
 }
}

答案是3

 


2 重寫hashCode和equals的方法

package cn.xy.test;

public class Point2
{
 private int x;
 private int y;

 public Point2(int x, int y)
 {
  super();
  this.x = x;
  this.y = y;
 }

 @Override
 public int hashCode()
 {
  final int prime = 31;
  int result = 1;
  result = prime * result + x;
  result = prime * result + y;
  return result;
 }

 @Override
 public boolean equals(Object obj)
 {
  if (this == obj) return true;
  if (obj == null) return false;
  if (getClass() != obj.getClass()) return false;
  Point2 other = (Point2) obj;
  if (x != other.x) return false;
  if (y != other.y) return false;
  return true;
 }

 public int getX()
 {
  return x;
 }

 public void setX(int x)
 {
  this.x = x;
 }

 public int getY()
 {
  return y;
 }

 public void setY(int y)
 {
  this.y = y;
 }

}

public class HashSetAndHashCode
{
 public static void main(String[] args)
 {
  HashSet<Point2> hs2 = new HashSet<Point2>();
  Point2 p21 = new Point2(3, 3);
  Point2 p22 = new Point2(3, 3);
  Point2 p23 = new Point2(3, 5);
  hs2.add(p21);
  hs2.add(p22);
  hs2.add(p23);
  System.out.println(hs2.size());
 }
}

答案是2。p21和p22被認為是同一個物件。

 


3 沒有重寫hashCode的方法,但重寫equals的方法

package cn.xy.test;

public class Point3
{
 private int x;
 private int y;

 public Point3(int x, int y)
 {
  super();
  this.x = x;
  this.y = y;
 }

 @Override
 public boolean equals(Object obj)
 {
  if (this == obj) return true;
  if (obj == null) return false;
  if (getClass() != obj.getClass()) return false;
  Point3 other = (Point3) obj;
  if (x != other.x) return false;
  if (y != other.y) return false;
  return true;
 }

 public int getX()
 {
  return x;
 }

 public void setX(int x)
 {
  this.x = x;
 }

 public int getY()
 {
  return y;
 }

 public void setY(int y)
 {
  this.y = y;
 }

}

public class HashSetAndHashCode
{
 public static void main(String[] args)
 {
  HashSet<Point3> hs3 = new HashSet<Point3>();
  Point3 p31 = new Point3(3, 3);
  Point3 p32 = new Point3(3, 3);
  Point3 p33 = new Point3(3, 5);
  hs3.add(p31);
  hs3.add(p32);
  hs3.add(p33);
  System.out.println(hs3.size());
 }
}

可能是2,可能是3。因為根據記憶體地址算出的hashcode不知道是否在一個區域。

 

 

           

給我老師的人工智慧教程打call!http://blog.csdn.net/jiangjunshow

這裡寫圖片描述