java中Integer比较需要注意的问题
package com.srie.test; import java.util.HashMap;
import java.util.Map; public class Test003 {
public static void main(String[] args) {
Map<String, Object> map = new HashMap<String, Object>();
map.put("i1", new Integer(1));
map.put("i2", new Integer(1));
Integer i1 = new Integer(1);
Integer i2 = new Integer(1);
Integer i3 = new Integer(2);
System.out.println(i1.compareTo(i2));
if(i3>=i1){
System.out.println("i3>i1");
}else{
System.out.println("i3<=i1");
}
if (i1 == i2) {
System.out.println("i1==i2");
} else {
System.out.println("i1!=i2");
}
Integer i1O = (Integer) map.get("i1");
Integer i2O = (Integer) map.get("i2");
if (i1O == i2O) {
System.out.println("i1O==i2O");
} else {
System.out.println("i1O!=i2O");
} }
}
使用List进行是否包含的判断情况:
public class Test006 {
public static void main(String[] args) {
List<Integer> list = new ArrayList<Integer>();
list.add(1); Integer i1 = new Integer(1);
Integer i2 = new Integer(1);
System.out.println(i1==i2); // false
System.out.println(list.contains(i1)); // true
System.out.println(list.contains(i2)); // true
}
}