Get method on LinkedHashMap modifies the hashmap

My understanding is that in typical java/groovy, the following code would output an empty hashmap:

    x = [:]
    x.get("hello", "world")
    print(x)

However, in Nextflow, the output is [hello:world]. Assuming this is a deliberate departure from Groovy and Java’s default behavior, are Nextflow’s modifications to the standard behavior of Java/Groovy objects documented ~comprehensively somewhere? And for this specific example, is there an elegant way to get the value stored under a key (or a default value if the key is not present) without modifying the hashmap?

This is the expected behavior in both Nextflow and Groovy. world will be returned because there is no key hello in the map. If there was one, then the value would be printed to the standard output. Check the example below:

x = ["hello":"marcel"]
x.get("hello", "world")
print(x)

Outputs:
CleanShot 2024-06-25 at 20.14.04@2x

But if the key is not in the map, as you’ve already seen:

x = [:]
x.get("hello", "world")
print(x)

CleanShot 2024-06-25 at 20.15.04@2x

If you use the get method with a single argument (the key), then you will get the empty map.
CleanShot 2024-06-25 at 20.16.09@2x

In Java, we have get and getOrDefault but in Groovy, if you give two parameters to get, you will get the some behavior as Java’s getOrDefault.

Gotcha, I found a java description of the get function for a LinkedHashMap and assumed groovy had the same behavior. Thanks for clarifying.

1 Like

You can refer to Groovy JDK Enhancements to see which methods are added by Groovy to the standard library.

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.