Java备忘
HashMap用自定义类作为key
参见:java HashMap用自定义类作为key
保证
能正确区分即可,为了加快速度可以再重载 1
public boolean equals(Object obj)
1
public int hashCode()
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
class IntPair{
private int i1;
private int i2;
public IntPair(int i1, int i2){
this.i1 = i1;
this.i2 = i2;
}
public boolean equals(Object obj){
if(this == obj)//判断是否是本类的一个引用
return true;
if(obj == null)//
return false;
IntPair pair = (IntPair)obj;
if(this.i1 != pair.i1)
return false;
if(this.i2 != pair.i2)
return false;
return true;
}
public int hashCode(){
int result = 17;
result = result * 31 + i1;
result = result * 31 + i2;
return result;
}
}
PreparedStatement insert BatchUpdateException
prepareStatement函数参数的sql语句不能以”;”结尾
Double.parseDouble(“1.00084e-06”)可以正确解析
实例化comparator报错“Comparison method violates its general contract!”
本来比较的是Double型变量,改用
即可1
Double.compare
1
2
3
4
5
6
7
ArrayList<Entry<IntPairUnique,Double>> al = new ArrayList<Entry<IntPairUnique,Double>>();
Collections.sort(al, new Comparator<Entry<IntPairUnique, Double>>(){
public int compare(Entry<IntPairUnique, Double> o1,
Entry<IntPairUnique, Double> o2) {
return Double.compare(o2.getValue(), o1.getValue());
}
});