40

假设我只希望在生成的 equals 和 hashCode 实现中包含一个或两个字段(或者可能排除一个或多个字段)。对于一个简单的类,例如:

data class Person(val id: String, val name: String)

Groovy 有这个:

@EqualsAndHashCode(includes = 'id')

龙目岛有这个:

@EqualsAndHashCode(of = "id")

在 Kotlin 中这样做的惯用方式是什么?

到目前为止我的方法

data class Person(val id: String) {
   // at least we can guarantee it is present at access time
   var name: String by Delegates.notNull()

   constructor(id: String, name: String): this(id) {
      this.name = name
   }
}

只是感觉不对……我真的不想name变得可变,而且额外的构造函数定义很丑陋。

4

10 回答 10

20

我用过这种方法。

data class Person(val id: String, val name: String) {
   override fun equals(other: Person) = EssentialData(this) == EssentialData(other)
   override fun hashCode() = EssentialData(this).hashCode()
   override fun toString() = EssentialData(this).toString().replaceFirst("EssentialData", "Person")
}

private data class EssentialData(val id: String) {
   constructor(person: Person) : this(id = person.id) 
}
于 2017-09-15T20:45:04.590 回答
8

这种方法可能适用于属性排除:

class SkipProperty<T>(val property: T) {
  override fun equals(other: Any?) = true
  override fun hashCode() = 0
}

SkipProperty.equals简单地返回 true,这会导致嵌入property被跳过equals父对象。

data class Person(
    val id: String, 
    val name: SkipProperty<String>
)
于 2020-02-22T16:20:49.740 回答
5

我也不知道 Kotlin(1.1)中的“惯用方式”来做到这一点......

我最终覆盖了equals并且hashCode

data class Person(val id: String,
                  val name: String) {

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

        other as Person

        if (id != other.id) return false

        return true
    }

    override fun hashCode(): Int {
        return id.hashCode()
    }
}

没有“更好”的方法吗?

于 2017-06-07T13:42:56.607 回答
3

这是一个有点创意的方法:

data class IncludedArgs(val args: Array<out Any>)

fun includedArgs(vararg args: Any) = IncludedArgs(args)


abstract class Base {
    abstract val included : IncludedArgs

    override fun equals(other: Any?) = when {
        this identityEquals other -> true
        other is Base -> included == other.included
        else -> false
    }

    override fun hashCode() = included.hashCode()

    override fun toString() = included.toString()
}

class Foo(val a: String, val b : String) : Base() {
    override val included = includedArgs(a)
}

fun main(args : Array<String>) {
    val foo1 = Foo("a", "b")
    val foo2 = Foo("a", "B")

    println(foo1 == foo2) //prints "true"
    println(foo1)         //prints "IncludedArgs(args=[a])"
}
于 2015-04-12T23:27:08.227 回答
3

这建立在@bashor 的方法之上,并使用私有主构造函数和公共辅助构造函数。遗憾的是,equals 要忽略的属性不能是 val,但可以隐藏 setter,因此从外部角度来看,结果是等效的。

data class ExampleDataClass private constructor(val important: String) {
  var notSoImportant: String = ""
    private set

  constructor(important: String, notSoImportant: String) : this(important) {
    this.notSoImportant = notSoImportant
  }
}
于 2018-07-27T08:17:38.223 回答
2

可重用的解决方案:为了有一种简单的方法来选择要包含在equals()和中的字段hashCode(),我编写了一个名为“stem”的小助手(基本核心数据,与平等相关)。

用法很简单,生成的代码非常小:

class Person(val id: String, val name: String) {
    private val stem = Stem(this, { id })

    override fun equals(other: Any?) = stem.eq(other)
    override fun hashCode() = stem.hc()
}

可以通过额外的即时计算来权衡存储在类中的支持字段:

    private val stem get() = Stem(this, { id })

由于Stem采用任何函数,因此您可以自由指定如何计算相等性。要考虑多个字段,只需为每个字段添加一个 lambda 表达式(可变参数):

    private val stem = Stem(this, { id }, { name })

执行:

class Stem<T : Any>(
        private val thisObj: T,
        private vararg val properties: T.() -> Any?
) {     
    fun eq(other: Any?): Boolean {
        if (thisObj === other)
            return true

        if (thisObj.javaClass != other?.javaClass)
            return false

        // cast is safe, because this is T and other's class was checked for equality with T
        @Suppress("UNCHECKED_CAST") 
        other as T

        return properties.all { thisObj.it() == other.it() }
    }

    fun hc(): Int {
        // Fast implementation without collection copies, based on java.util.Arrays.hashCode()
        var result = 1

        for (element in properties) {
            val value = thisObj.element()
            result = 31 * result + (value?.hashCode() ?: 0)
        }

        return result
    }

    @Deprecated("Not accessible; use eq()", ReplaceWith("this.eq(other)"), DeprecationLevel.ERROR)
    override fun equals(other: Any?): Boolean = 
        throw UnsupportedOperationException("Stem.equals() not supported; call eq() instead")

    @Deprecated("Not accessible; use hc()", ReplaceWith("this.hc(other)"), DeprecationLevel.ERROR)
    override fun hashCode(): Int = 
        throw UnsupportedOperationException("Stem.hashCode() not supported; call hc() instead")
}

如果您想知道最后两种方法,它们的存在会使以下错误代码在编译时失败:

override fun equals(other: Any?) = stem.equals(other)
override fun hashCode() = stem.hashCode()

如果这些方法被隐式调用或通过反射调用,则异常只是一个后备;必要时可以争论。

当然,Stem该类可以进一步扩展以包括自动生成toString()等。

于 2019-05-08T06:57:50.833 回答
0

更简单,更快,查看那里,或查看 Kotlin 文档。 https://discuss.kotlinlang.org/t/ignoring-certain-properties-when-generating-equals-hashcode-etc/2715/2 仅考虑主构造函数中的字段来构建自动访问方法,如equals等上。把无意义的留在外面。

于 2021-05-25T15:22:03.303 回答
0

您可以通过定义枚举来创建一个注释,该注释表示将属性作为参数排除@ExcludeToString或与参数一起排除。@ToString(Type.EXCLUDE)

然后使用反射格式的值getToString()

@Target(AnnotationTarget.FIELD)
@Retention(AnnotationRetention.RUNTIME)
annotation class ExcludeToString

data class Test(
        var a: String = "Test A",
        @ExcludeToString var b: String = "Test B"
) {
    override fun toString(): String {
        return ExcludeToStringUtils.getToString(this)
    }
}

object ExcludeToStringUtils {

    fun getToString(obj: Any): String {
        val toString = LinkedList<String>()
        getFieldsNotExludeToString(obj).forEach { prop ->
            prop.isAccessible = true
            toString += "${prop.name}=" + prop.get(obj)?.toString()?.trim()
        }
        return "${obj.javaClass.simpleName}=[${toString.joinToString(", ")}]"
    }

    private fun getFieldsNotExludeToString(obj: Any): List<Field> {
        val declaredFields = obj::class.java.declaredFields
        return declaredFields.filterNot { field ->
            isFieldWithExludeToString(field)
        }
    }

    private fun isFieldWithExludeToString(field: Field): Boolean {
        field.annotations.forEach {
            if (it.annotationClass == ExcludeToString::class) {
                return true
            }
        }
        return false
    }

}

总帐

要旨

于 2020-07-27T03:15:37.043 回答
0

考虑以下用于实现 equals/hashcode 的通用方法。由于使用了内联和 kotlin 值类,下面的代码应该不会影响性能:

@file:Suppress("EXPERIMENTAL_FEATURE_WARNING")

package org.beatkit.common

import kotlin.jvm.JvmInline

@Suppress("NOTHING_TO_INLINE")
@JvmInline
value class HashCode(val value: Int = 0) {
    inline fun combineHash(hash: Int): HashCode = HashCode(31 * value + hash)
    inline fun combine(obj: Any?): HashCode = combineHash(obj.hashCode())
}

@Suppress("NOTHING_TO_INLINE")
@JvmInline
value class Equals(val value: Boolean = true) {
    inline fun combineEquals(equalsImpl: () -> Boolean): Equals = if (!value) this else Equals(equalsImpl())
    inline fun <A : Any> combine(lhs: A?, rhs: A?): Equals = combineEquals { lhs == rhs }
}

@Suppress("NOTHING_TO_INLINE")
object Objects {
    inline fun hashCode(builder: HashCode.() -> HashCode): Int = builder(HashCode()).value

    inline fun hashCode(vararg objects: Any?): Int = hashCode {
        var hash = this
        objects.forEach {
            hash = hash.combine(it)
        }
        hash
    }

    inline fun hashCode(vararg hashes: Int): Int = hashCode {
        var hash = this
        hashes.forEach {
            hash = hash.combineHash(it)
        }
        hash
    }

    inline fun <T : Any> equals(
        lhs: T,
        rhs: Any?,
        allowSubclasses: Boolean = false,
        builder: Equals.(T, T) -> Equals
    ): Boolean {
        if (rhs == null) return false
        if (lhs === rhs) return true
        if (allowSubclasses) {
            if (!lhs::class.isInstance(rhs)) return false
        } else {
            if (lhs::class != rhs::class) return false
        }
        @Suppress("unchecked_cast")
        return builder(Equals(), lhs, rhs as T).value
    }
}

有了这个,你可以很容易地以统一的方式实现/覆盖任何 equals/hashcode 实现:

data class Foo(val title: String, val bytes: ByteArray, val ignore: Long) {
    override fun equals(other: Any?): Boolean {
        return Objects.equals(this, other) { lhs, rhs ->
            this.combine(lhs.title, rhs.title)
                .combineEquals { lhs.bytes contentEquals rhs.bytes }
            // ignore the third field for equals
        }
    }

    override fun hashCode(): Int {
        return Objects.hashCode(title, bytes) // ignore the third field for hashcode
    } 
}
于 2021-10-11T15:39:29.323 回答
0

如果您不想接触数据类,这是另一种 hacky 方法。
您可以重用整个equals()数据类,同时排除某些字段。
只是copy()排除字段具有固定值的类:

data class Person(val id: String,
                  val name: String)
fun main() {
    val person1 = Person("1", "John")
    val person2 = Person("2", "John")
    println("Full equals: ${person1 == person2}")
    println("equals without id: ${person1.copy(id = "") == person2.copy(id = "")}")
   
}

输出:

Full equals: false
equals without id: true
于 2022-02-25T15:29:35.383 回答