JAVA长期比较看似不准确

时间:2015-07-02 16:33:51

标签: java equality

有一个问题我需要与长值进行比较并检查是否相等。我的相关代码如下:

for(int i = 0; i <=results.size()-1; i++) {
        if(results.get(i).get(0).getTime() == results.get(i).get(results.get(i).size()-1).getTime()){
            continue;
        }
}

设置断点时,

results.get(i).get(0).getTime()     has value: 1329286731000
results.get(i).get(results.get(i).size()-1).getTime()     has value: 1329286731000

然而比较返回false。这里有什么想法吗?

注意:结果是List&gt;和LocationEvent类可以在下面找到:

public class LocationEvent implements Serializable {

private static final long serialVersionUID = 1L;

/**
 * The unique identifier for this event
 */
private Integer id;

/**
 * The date and time that this location event was recorded in milliseconds since January 1, 1970, 00:00:00 GMT
 */
private Long time;

/**
 * The latitude at which the event occurred
 */
private Double latitude;

/**
 * * The longitude at which the event occurred
 */
private Double longitude;

/**
 * The speed the resource was traveling at the time of the event in MPH
 */
private Double speed;

/**
 * The direction the resource was traveling at the time of the event (i.e. N, S, NW, SE, etc).
 */
private String direction;

/**
 * The altitude at which the event occurred in feet above sea level
 */
private Double altitude;

/**
 * The closest known address to the location.
 */
private String nearestAddress;

/**
 * Any available comment describing this location event
 */
private String comment;

/**
 * The source of this event (i.e. GeoProductSolutions, GpsInsight, Multispeak, WirelessMatrix, WiSys, NiscMobile,
 * AppSuite-iOS, AppSuite-Android)
 */
private String source;

/**
 * The device to which this event is associated
 */
private LocationDeviceSummary device;

public Integer getId() {
    return id;
}

public void setId(Integer id) {
    this.id = id;
}

public Long getTime() {
    return time;
}

public void setTime(Long time) {
    this.time = time;
}

public Double getLatitude() {
    return latitude;
}

public void setLatitude(Double latitude) {
    this.latitude = latitude;
}

public Double getLongitude() {
    return longitude;
}

public void setLongitude(Double longitude) {
    this.longitude = longitude;
}

public Double getSpeed() {
    return speed;
}

public void setSpeed(Double speed) {
    this.speed = speed;
}

public String getDirection() {
    return direction;
}

public void setDirection(String direction) {
    this.direction = direction;
}

public Double getAltitude() {
    return altitude;
}

public void setAltitude(Double altitude) {
    this.altitude = altitude;
}

public String getNearestAddress() {
    return nearestAddress;
}

public void setNearestAddress(String nearestAddress) {
    this.nearestAddress = nearestAddress;
}

public String getComment() {
    return comment;
}

public void setComment(String comment) {
    this.comment = comment;
}

public String getSource() {
    return source;
}

public void setSource(String source) {
    this.source = source;
}

public LocationDeviceSummary getDevice() {
    return device;
}

public void setDevice(LocationDeviceSummary device) {
    this.device = device;
}
}

2 个答案:

答案 0 :(得分:1)

如果您的结果是Long而不是long s,则==运算符可能会返回false,无论值是否相等。

您可能希望使用equals或与longValue()一起调用==进行比较。

注意

如果在范围Byte.MIN_VALUE <= x <- Byte.MAX_VALUE范围内,则会缓存整数值,因此该范围内的==比较将返回true

答案 1 :(得分:0)

检查这两个例子:

public class Test {
    public static void main(String[] args) {
        Long x=1329286731000l;
        Long y=1329286731000l;
        System.out.println(y==x);
    }
}

返回 false ,因为它们等于但它们是不同的对象:

public class Test {
    public static void main(String[] args) {
        long x=1329286731000l;
        long y=1329286731000l;
        System.out.println(y==x);
    }
}

它返回true,因为它比较了值。

你的get(0).getTime()返回一个Long,所以你必须以下面的方式使用equals:

public class Test {
    public static void main(String[] args) {
        Long x=1329286731000l;
        Long y=1329286731000l;
        System.out.println(y.equals(x));
    }
}