Mike Baxter
Mike Baxter

Reputation: 7258

Using `Objects.equals()` in Android

I am trying to use the Objects.equals(obj a, obj b) method (link) in Android, but it seems Android does not have access to it. As far as I'm aware, this class was available in Java 1.7 and later. Is there any way to have access to this class in Android? Or is there an equivalent method that behaves the same way that I can use instead?

Upvotes: 5

Views: 6427

Answers (7)

francas
francas

Reputation: 331

You can use the compat class ObjectsCompat.equals which is supported from v4.

Upvotes: 1

Ashok Kumar
Ashok Kumar

Reputation: 131

Use StringValue.equals(Object o); It may help.

Upvotes: 1

Tomate_Salat
Tomate_Salat

Reputation: 78

You can use java 7 with compile level 19 (without try-resources if below min-level 19). You can change the compile-Options with gradle by setting the compileOptions:

compileOptions {
    sourceCompatibility JavaVersion.VERSION_1_7
    targetCompatibility JavaVersion.VERSION_1_7
}

Here is a full example:

buildscript {
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:0.8.+'
    }
}
apply plugin: 'android'

repositories {
    mavenCentral()
}

android {
    compileSdkVersion 19
    buildToolsVersion "19.0.0"

    defaultConfig {
        minSdkVersion 7
        targetSdkVersion 16
        versionCode 1
        versionName "1.0"
    }

    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_7
        targetCompatibility JavaVersion.VERSION_1_7
    }
    buildTypes {
        release {
            runProguard false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
        }
    }
}

dependencies {
    compile 'com.android.support:appcompat-v7:+'
}

Upvotes: 1

Loïc
Loïc

Reputation: 591

The javadoc for Objects.equals(obj a, obj b) says: Returns true if the arguments are equal to each other and false otherwise. Consequently, if both arguments are null, true is returned and if exactly one argument is null, false is returned. Otherwise, equality is determined by using the equals method of the first argument.

which is the equivalent to:

if (a == null && b == null) {
   return true; 
} else if (a == null || b == null) {
   return false; 
} else return a.equals(b);

Upvotes: 6

gowtham
gowtham

Reputation: 987

If You can use Apache objectutils

ObjectUtils.equals(Object object1, Object object2) -- Returns boolean

Compares two objects for equality, where either one or both objects may be null.

 ObjectUtils.equals(null, null)                  = true
 ObjectUtils.equals(null, "")                    = false
 ObjectUtils.equals("", null)                    = false
 ObjectUtils.equals("", "")                      = true
 ObjectUtils.equals(Boolean.TRUE, null)          = false
 ObjectUtils.equals(Boolean.TRUE, "true")        = false
 ObjectUtils.equals(Boolean.TRUE, Boolean.TRUE)  = true
 ObjectUtils.equals(Boolean.TRUE, Boolean.FALSE) = false

Upvotes: 0

Ahmad
Ahmad

Reputation: 72533

You can call equals() on an Object:

if(object1.equals(object2)){ 
    // Do something 
}

Upvotes: 1

Joetjah
Joetjah

Reputation: 6132

Objects.equals() has the following source:

public static boolean equals(Object a, Object b) {
     return (a == b) || (a != null && a.equals(b));
 }

Upvotes: 9

Related Questions