You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@groovy.apache.org by "Paul King (Jira)" <ji...@apache.org> on 2020/09/01 08:11:00 UTC

[jira] [Commented] (GROOVY-9710) List.intersect changes behavior from Groovy 2.4 and Groovy 2.5+

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

Paul King commented on GROOVY-9710:
-----------------------------------

Actually, the order in 2.4 is determined by whichever of the first or second collections in {{first.intersect(second)}} is bigger. It was a marginal efficiency gain in early JDKs.
{code:java}
// Groovy 1.5-2.4
def items = ['a', 'b', 'c', 'd']
assert items.intersect(['c', 'b']) == ['c', 'b'] // order from argument (second) since it is smaller
assert items.intersect([1, 2, 3, 'c', 'b', 4, 5, 6]) == ['b', 'c']  // order preserved from items (first) since items is smaller
{code}
Now, the order is fixed. Obviously it is now in the reverse direction to what you were expecting. While we could say the order is undefined, I can also see the argument for preserving the order.


> List.intersect changes behavior from Groovy 2.4 and Groovy 2.5+
> ---------------------------------------------------------------
>
>                 Key: GROOVY-9710
>                 URL: https://issues.apache.org/jira/browse/GROOVY-9710
>             Project: Groovy
>          Issue Type: Bug
>          Components: groovy-runtime
>    Affects Versions: 3.0.5, 2.5.13
>         Environment: Ubuntu 18.04.5 LTS
> OpenJDK Java 1.8.0_265 (Private Build 25.265-b01)
> Gradle 5.6.3
>            Reporter: Sam Gleske
>            Priority: Major
>
> h2. Description
> [List.intersect|http://docs.groovy-lang.org/latest/html/groovy-jdk/java/util/List.html#intersect(java.lang.Iterable)] preserves the ordering of the source list in Groovy 2.4 but instead takes the ordering of the intersecting list. This can lead to unexpected behavior in Groovy applications as code migrates to newer versions of Groovy.
> h2. Example code
> {code:java}
> // Passes assertion in Groovy 2.4.20 but fails in Groovy 2.5.13 and 3.0.5
> 0
> assert ['a', 'b'].intersect(['c', 'b', 'a']) == ['a', 'b']{code}
> h2. Expected behavior
> The order of the first list is preserved.
> h2. Actual behavior
> The order of the second list takes precedence.
> h2. Groovy version matrix
> ||Groovy version||Behavior||
> |2.4.20|Assertion passes|
> |2.5.13|Assertion fails (order reversed) |
> |3.0.5|Assertion fails (order reversed) |



--
This message was sent by Atlassian Jira
(v8.3.4#803005)