![](/img/trans.png)
[英]About Apache Commons EqualsBuilder and HashCodeBuilder and null values
[英]Apache Commons Lang HashCodeBuilder collision
我與使用3.4版的Apache Commons Lang HashCodeBuilder發生沖突。 我正在哈希一個Route對象,其中包含兩個Cell對象,開始和結束。 最后,我提供一個發生沖突時的示例。 這兩個類都覆蓋hashCode和equals方法。 首先是Cell類:
import org.apache.commons.lang3.builder.EqualsBuilder;
import org.apache.commons.lang3.builder.HashCodeBuilder;
public class Cell {
private int east;
private int south;
public Cell(int east, int south) {
this.east = east;
this.south = south;
}
public int getEast() {
return east;
}
public void setEast(int east) {
this.east = east;
}
public int getSouth() {
return south;
}
public void setSouth(int south) {
this.south = south;
}
@Override
/**
* Compute hash code by using Apache Commons Lang HashCodeBuilder.
*/
public int hashCode() {
return new HashCodeBuilder(17, 31)
.append(this.south)
.append(this.east)
.toHashCode();
}
@Override
/**
* Compute equals by using Apache Commons Lang EqualsBuilder.
*/
public boolean equals(Object obj) {
if (!(obj instanceof Cell))
return false;
if (obj == this)
return true;
Cell cell = (Cell) obj;
return new EqualsBuilder()
.append(this.south, cell.south)
.append(this.east, cell.east)
.isEquals();
}
}
和Route類:
import org.apache.commons.lang3.builder.EqualsBuilder;
import org.apache.commons.lang3.builder.HashCodeBuilder;
import java.util.*;
public class Route {
private Cell startCell;
private Cell endCell;
public Route(Cell startCell, Cell endCell) {
this.startCell = startCell;
this.endCell = endCell;
}
public Cell getStartCell() {
return startCell;
}
public void setStartCell(Cell startCell) {
this.startCell = startCell;
}
public Cell getEndCell() {
return endCell;
}
public void setEndCell(Cell endCell) {
this.endCell = endCell;
}
@Override
public int hashCode() {
return new HashCodeBuilder(43, 59)
.append(this.startCell)
.append(this.endCell)
.toHashCode();
}
@Override
public boolean equals(Object obj) {
if (!(obj instanceof Route))
return false;
if (obj == this)
return true;
Route route = (Route) obj;
return new EqualsBuilder()
.append(this.startCell, route.startCell)
.append(this.endCell, route.endCell)
.isEquals();
}
}
碰撞示例:
public class Collision {
public static void main(String[] args) {
Route route1 = new Route(new Cell(154, 156), new Cell(154, 156));
Route route2 = new Route(new Cell(153, 156), new Cell(151, 158));
System.out.println(route1.hashCode() + " " + route2.hashCode());
}
}
輸出為1429303 1429303 。 現在,如果將兩個類的初始奇數和乘數奇數更改為相同,則此示例不會沖突。 但是在HashCodeBuilder的文檔中,它明確指定:
必須傳遞兩個隨機選擇的奇數。理想情況下,每個類的奇數應該不同 ,但這並不是至關重要的。
理想情況下,如果可能的話,我希望為示例提供完美的哈希函數 (內插函數)。
在Java中,哈希碼綁定到Integer(32位)范圍,因此,如果您有2 ^ 62個以上的對象,則將發生沖突(如果具有理想的分布,則將發生沖突)。 但是實際上,由於哈希碼不能提供完美的分布,沖突發生的頻率更高。
您可能能夠通過在生成哈希代碼時添加更多參數來更好地分配生成的哈希代碼(這與Apache commons庫無關)。 通過此示例,您可以預先計算Route
類的一個或多個屬性 ,並在生成哈希碼時使用此屬性。 例如,計算兩個Cell
對象之間的直線的斜率:
double slope = (startCell.getEast() - endCell.getEast());
if ( slope == 0 ){//prevent division by 0
slope = startCell.getSouth() - endCell.getSouth();
}else{
slope = (startCell.getSouth() - endCell.getSouth()) / slope;
}
return new HashCodeBuilder(43, 59)
.append(this.startCell)
.append(this.endCell)
.append(slope)
.toHashCode();
用您的示例生成83091911 83088489 。 或者(或與之一起)使用兩個Cell
對象之間的距離:
double length = Math.sqrt(Math.pow(startCell.getSouth() - endCell.getSouth(), 2) + Math.pow(startCell.getEast() - endCell.getEast(), 2));
return new HashCodeBuilder(43, 59)
.append(this.startCell)
.append(this.endCell)
.append(length)
.toHashCode();
與您的示例一起使用的結果為83091911和-486891382 。
並測試這是否可以防止碰撞:
List<Cell> cells = new ArrayList<Cell>();
for ( int i = 0; i < 50; i++ ){
for ( int j = 0; j < 50; j++ ){
Cell c = new Cell(i,j);
cells.add(c);
}
}
System.out.println(cells.size() + " cells generated");
System.out.println("Testing " + (cells.size()*cells.size()) + " number of Routes");
Set<Integer> set = new HashSet<Integer>();
int collisions = 0;
for ( int i = 0; i < cells.size(); i++ ){
for ( int j = 0; j < cells.size(); j++ ){
Route r = new Route(cells.get(i), cells.get(j));
if ( set.contains(r.hashCode() ) ){
collisions++;
}
set.add(r.hashCode());
}
}
System.out.println(collisions);
在生成的6,250,000條路線中:
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.