You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@groovy.apache.org by "Albert Fernández Marsal (JIRA)" <ji...@apache.org> on 2018/12/19 07:40:00 UTC

[jira] [Comment Edited] (GROOVY-8929) Inconsistent equals and hashCode for arrays

    [ https://issues.apache.org/jira/browse/GROOVY-8929?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=16723998#comment-16723998 ] 

Albert Fernández Marsal edited comment on GROOVY-8929 at 12/19/18 7:39 AM:
---------------------------------------------------------------------------

But java keeps consistency with equals:
{code}
13:21:~$ jshell
|  Welcome to JShell -- Version 9.0.1
|  For an introduction type: /help intro

jshell> int [] a = new int[] {1,2}
a ==> int[2] { 1, 2 }

jshell> int [] b = new int[] {1,2}
b ==> int[2] { 1, 2 }

jshell> a.equals(b)
$3 ==> false

jshell> Arrays.equals(a,b)
$4 ==> true

jshell> a.hashCode()
$4 ==> 1540011289

jshell> b.hashCode()
$5 ==> 1596000437
{code}

The main problem is that an array can't be used in a hash based data structure (this is how I found out)


was (Author: afmarsal):
But java keeps consistency with equals:
{code}
13:21:~$ jshell
|  Welcome to JShell -- Version 9.0.1
|  For an introduction type: /help intro

jshell> int [] a = new int[] {1,2}
a ==> int[2] { 1, 2 }

jshell> int [] b = new int[] {1,2}
b ==> int[2] { 1, 2 }

jshell> a.equals(b)
$3 ==> false

jshell> Arrays.equals(a,b)
$4 ==> true

jshell> a.hashCode()
$4 ==> 1540011289

jshell> b.hashCode()
$5 ==> 1596000437
{code}

> Inconsistent equals and hashCode for arrays
> -------------------------------------------
>
>                 Key: GROOVY-8929
>                 URL: https://issues.apache.org/jira/browse/GROOVY-8929
>             Project: Groovy
>          Issue Type: Bug
>    Affects Versions: 2.4.14
>            Reporter: Albert Fernández Marsal
>            Priority: Major
>
> {code}
> 09:29:~$ groovysh
> Groovy Shell (2.4.14, JVM: 1.8.0_171)
> Type ':help' or ':h' for help.
> ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
> # int[]
> groovy:000> a = [1,2,3] as int[]
> ===> [1, 2, 3]
> groovy:000> a.class
> ===> class [I
> groovy:000> b = [1,2,3] as int[]
> ===> [1, 2, 3]
> groovy:000> b.class
> ===> class [I
> groovy:000> a == b
> ===> true
> groovy:000> a.hashCode()
> ===> 1329315688
> groovy:000> b.hashCode()
> ===> 1489933928
> # String[]
> groovy:000> c = ['a', 'b'] as String[]
> ===> [a, b]
> groovy:000> d = ['a', 'b'] as String[]
> ===> [a, b]
> groovy:000> c == d
> ===> true
> groovy:000> c.hashCode()
> ===> 319689067
> groovy:000> d.hashCode()
> ===> 6422064
> groovy:000>
> {code}



--
This message was sent by Atlassian JIRA
(v7.6.3#76005)