https://kotlinlang.org logo
#android
Title
# android
k

kirillrakhman

05/03/2017, 2:15 PM
@audhil this code
Copy code
public class DataClass {

    private final String a;
    private final int b;

    public DataClass(final String a, final int b) {
        this.a = a;
        this.b = b;
    }

    public String getA() {
        return a;
    }

    public int getB() {
        return b;
    }

    @Override
    public boolean equals(final Object o) {
        if (this == o)
            return true;
        if (o == null || getClass() != o.getClass())
            return false;

        DataClass dataClass = (DataClass) o;

        if (b != dataClass.b)
            return false;
        return a != null ? a.equals(dataClass.a) : dataClass.a == null;
    }

    @Override
    public int hashCode() {
        int result = a != null ? a.hashCode() : 0;
        result = 31 * result + b;
        return result;
    }
}
is converted to
Copy code
class DataClass(val a: String?, val b: Int) {

    override fun equals(o: Any?): Boolean {
        if (this === o)
            return true
        if (o == null || javaClass != o.javaClass)
            return false

        val dataClass = o as DataClass?

        if (b != dataClass!!.b)
            return false
        return if (a != null) a == dataClass.a else dataClass.a == null
    }

    override fun hashCode(): Int {
        var result = a?.hashCode() ?: 0
        result = 31 * result + b
        return result
    }
}
you just have to add the
data
modifier and remove
equals
and
hashCode
yourself
👍 1
a

audhil

05/03/2017, 2:16 PM
kirillrakhman: thank you so much, I think this'll work
@kirillrakhman
k

kirillrakhman

05/03/2017, 2:19 PM
@audhil if you remove
equals
and
hashCode
beforehand, it's even easier
1
f

firezenk

05/03/2017, 3:14 PM
Intellij itself has the action “Covert to kotlin”, and do the exact task that you need
k

kirillrakhman

05/03/2017, 7:02 PM
that's what I was talking about, but I guess I could have made that clearer