是的,它确实是一个按位异或运算符。我尝试使用 ^ 运算符对 hashcode() 方法 & 得到相同的结果。
import java.util.*;
class TreeMapExample {
public static void main(String args[]) {
// Creating TreeMap object
TreeMap<String, Integer> tm = new TreeMap<String, Integer>();
// Adding elements to the Map
tm.put("Chaitanya", 27);
tm.put("Raghu", 35);
tm.put("Rajeev", 37);
tm.put("Syed", 28);
tm.put("Hugo", 32);
// Getting a set of the entries
Set set = tm.entrySet();
// Get an iterator
Iterator it = set.iterator();
// Display elements
int hash;
while(it.hasNext()) {
Map.Entry me = (Map.Entry)it.next();
System.out.println("Key: "+me.getKey() + " & Value: "+me.getValue());
System.out.println("hashcode value by method : "+me.hashCode());
hash = me.getKey().hashCode() ^ me.getValue().hashCode();
System.out.println("hashcode value by operator : "+me.hashCode()+"\n");
}
}
}