Java 理解排序的HashCode&;未排序的ArrayList<;对象>;
我在做一个小项目,遇到了一个我无法解释的问题。这是我的代码: ==Main=====Java 理解排序的HashCode&;未排序的ArrayList<;对象>;,java,sorting,arraylist,integer,hashcode,Java,Sorting,Arraylist,Integer,Hashcode,我在做一个小项目,遇到了一个我无法解释的问题。这是我的代码: ==Main===== Beacon b1 = new Beacon("192.168.0.12", 72.0); Beacon b4 = new Beacon("192.168.0.13", 72.0); Beacon b2 = new Beacon("192.168.0.24", 84.0); Beacon b3 = new Beacon("192.168.0.5", 64.0); Arra
Beacon b1 = new Beacon("192.168.0.12", 72.0);
Beacon b4 = new Beacon("192.168.0.13", 72.0);
Beacon b2 = new Beacon("192.168.0.24", 84.0);
Beacon b3 = new Beacon("192.168.0.5", 64.0);
ArrayList<Beacon> alBeacons = new ArrayList();
alBeacons.add(b4);
alBeacons.add(b2);
alBeacons.add(b1);
alBeacons.add(b3);
Room room = new Room("Testroom", alBeacons);
房间====
public Room(String sName, ArrayList<Beacon> alBeacons){
System.out.println("Unsorted: " + alBeacons.hashCode());
for(Beacon b: alBeacons){
System.out.println(b.getID());
}
alBeacons.sort(new Comparator<Beacon>() {
@Override
public int compare(Beacon o1, Beacon o2) {
return o1.getID() - o2.getID();
}
});
System.out.println("Sorted: " + alBeacons.hashCode());
for(Beacon b: alBeacons){
System.out.println(b.getID());
}
}
根据内容和顺序计算列表的哈希代码
public int hashCode() {
int hashCode = 1;
for (E e : this)
hashCode = 31*hashCode + (e==null ? 0 : e.hashCode());
return hashCode;
}
列表的hashCode
必须是(接口内的合同):
因此,元素的顺序对于确定hashcode很重要:为已排序或未排序的列表获取不同的hashcode是正常的
但是,您声明“根据ArrayList最初的排序方式,排序后我会得到不同的哈希代码”:因此基本上您声明:
- 案例1: 从[b,a,c]开始,排序为[a,b,c],得到散列码X
- 案例2: 从[c,b,a]开始,排序为[a,b,c],得到哈希代码Y
信标
类的hashcode
方法依赖于信标
的字段,则不应发生这种情况。您没有重写hashCode,因此它是“通常通过将对象的内部地址转换为int来实现”(来自javadoc)的基本实现
要解决您的问题,您应该在信标
中定义自己的hashCode
:
public int hashCode() {
final int prime = 31;
int res = (sIP != null) ? sIP.hashCode() : 1;
res = res * prime + ID;
return res * prime + Double.hashCode(RSSi);
}
你能澄清一下你的目标是什么吗?你在说什么哈希代码?列表的哈希代码?为什么您会惊讶于它的哈希代码会根据所包含的内容而变化?你为什么在乎?你能给我们看一下整个信标代码吗?@TimBiegeleisen我在做一个室内定位引擎,为了速度,我给每个房间一个哈希。当我需要查看房间里是否有人时,我可以比较hashCode。不能单独使用hashCode来指示列表中的内容。Hashcodes不是唯一的,并且不能保证在正确实现时除了与
equals()
兼容之外,还具有任何特定的属性。问题是alBeacons unsorted始终提供相同的hashCode()。如果我对它进行排序,我总是会得到一个不同的hashCode(),这取决于它之前的未排序程度。啊,非常好。我希望散列能够在排序和ArrayList的内部变量中工作。看起来我可能需要实现我自己的.equals(),从这个答案中我可以得到:非常感谢您的澄清@是的,如果您定义自己的hashCode;,则应始终覆盖等于我刚刚创建了一个单独的方法equals(信标b),因为任何其他比较都不应该起作用。考虑到每个项目可能的信标的大小(绝对最大值约为80个),最大值不应该是任何冲突。非常感谢你的帮助,从这个答案中学到了很多@没问题:)
public int hashCode() {
int hashCode = 1;
for (E e : this)
hashCode = 31*hashCode + (e==null ? 0 : e.hashCode());
return hashCode;
}
int hashCode = 1;
for (E e : list)
hashCode = 31*hashCode + (e==null ? 0 : e.hashCode());
return hashCode;
public int hashCode() {
final int prime = 31;
int res = (sIP != null) ? sIP.hashCode() : 1;
res = res * prime + ID;
return res * prime + Double.hashCode(RSSi);
}