hashCode and equals should depend on the same fields.

On Wed, Mar 22, 2023 at 8:02 AM Humphrey Lopez <hmmlo...@gmail.com> wrote:

> Hello, when having a key which equals another key, when trying to retrieve
> from cache it does not return the expected value. Is this a bug?
>
> I have a reproducible below in kotlin but in java we get the same result
> (test with Ignite 2.10 and  2.14) and java 11 and 19.
>
>
> import org.apache.ignite.Ignition
> import org.apache.ignite.configuration.CacheConfiguration
> import org.assertj.core.api.SoftAssertions
> import org.junit.jupiter.api.Test
>
> class MyTest {
>
>     private val key1 = MyKey("ABC", "DEF")
>     private val key2 = MyKey("ABC", "xxx")
>
>     @Test
>     fun testEquals() {
>         SoftAssertions.assertSoftly {
>             it.assertThat(key1).isEqualTo(key2)
>             it.assertThat(key1 == key2).isTrue
>         }
>     }
>
>     @Test
>     fun testWithMap() {
>         val map = mapOf(Pair(key1, "key1"))
>
>         SoftAssertions.assertSoftly {
>             it.assertThat(map.containsKey(key1)).isTrue
>             it.assertThat(map.containsKey(key2)).isTrue
>         }
>
>     }
>
>     @Test
>     fun testWithIgnite() {
>         val ignite = Ignition.start();
>         val cache = ignite.createCache(CacheConfiguration<MyKey, 
> String>("mycache"))
>
>         cache.put(key1, "key1")
>         SoftAssertions.assertSoftly {
>             it.assertThat(cache.containsKey(key1)).isTrue
>             it.assertThat(cache.containsKey(key2)).isTrue
>         }
>     }
>
>     private data class MyKey(val id: String, val type: String) {
>         override fun equals(other: Any?): Boolean {
>             if (other is MyKey)
>                 return id == other.id
>             return false
>         }
>
>         override fun hashCode(): Int {
>             return 1
>         }
>     }
> }
>
>

Reply via email to