Remove duplicates maps equal to condition Groovy / Grails - groovy

I would like to remove duplicates maps equal to some conditions, I have the maps below:
def map = [[name: "111F", invoice:"40",bilD:"aaaa", title:null],[name: "111F", invoice:"40",bilD:"aaaa",title:"something"],[name: "111F", invoice:"40",bilD:"bbbb",title:"something"]]
I tried with the code below to do that, but its not working
def newMap = map.unique {a, b -> (a.name == b.name && a.invoice == b.invoice && a.bilD == b.bilD)}
How can I get a map like below
[
[name: "111F", invoice:"40",bilD:"aaaa",title:"somethingOrNull"],[name: "111F", invoice:"40",bilD:"bbbb",title:"something"]
]

The easiest way I can think of is, add all the elements to a set, then transform the resulting set back to list (note, that your variable map actually contains a list).
The shortest way I can think of would be calling:
def newMap = (map as Set) as List

Collection.unique(Closure) behaves differently depending on the number of parameters in the closure. When the closure has one parameter it expects...
...a value used for comparison (either using
Comparable#compareTo(java.lang.Object) or
Object#equals(java.lang.Object)).
However, if the closure contains two arguments, as shown in your example, then the closure must return an integer...
...with 0 indicating the items are not unique
Strangely, the two-argument closure behavior seems to be in exact opposite of how it's described. Here's a working example:
def maps = [
[name: "111F", invoice:"40",bilD:"aaaa"],
[name: "111F", invoice:"40",bilD:"aaaa"],
[name: "111F", invoice:"40",bilD:"bbbb"]
]
assert maps.unique(false) {a, b ->
a.name == b.name && a.invoice == b.invoice && a.bilD == b.bilD ? 0 : 1
} == [['name':'111F', 'invoice':'40', 'bilD':'aaaa'], ['name':'111F', 'invoice':'40', 'bilD':'bbbb']]
However, in your case, you can simply use unique() without the closure:
assert maps.unique(false) == [['name':'111F', 'invoice':'40', 'bilD':'aaaa'], ['name':'111F', 'invoice':'40', 'bilD':'bbbb']]

Related

Groovy way to merge two lists of maps with some calculations

I need to merge to maps while perform some calculation for example having the following maps that always will be the same size
def map1 = [
[name: 'Coord1', quota: 200],
[name: 'Coord2', quota: 300]
]
def map2 = [
[name: 'Coord1', copiesToDate: 270],
[name: 'Coord2', copiesToDate: 30]
]
I want to get this map
def map3 = [
[name: 'Coord1', quota: 200, copiesToDate: 60, balance: 140],
[name: 'Coord2', quota: 300, copiesToDate: 30, balance: 270]
]
Right now i am trying with this solution and its working
def map4 = map1.collect { m1 ->
[
name: m1.name,
quota: m1.quota,
copiesToDate: map2.find { m2 ->
m1.name == m2.name
}.copiesToDate,
balanceToDate: m1.quota - map2.find { m2 ->
m1.name == m2.name
}.copiesToDate
]}
Could you please share a groovy way to do this task. Thanks
Grooviest code I could come up with:
def map3 = [map1, map2].transpose()*.sum().each { m ->
m.balance = m.quota - m.copiesToDate
}
edit: as noted by Tim, this code works as long as the two input lists (map1 and map2) are of the same size and have the maps in order. If this is not the case I would recommend Tim's answer which handles those cases.
The above returns the map as defined in your question. The following code:
def list1 = [
[name: 'Coord1', quota: 200],
[name: 'Coord2', quota: 300]
]
def list2 = [
[name: 'Coord1', copiesToDate: 60],
[name: 'Coord2', copiesToDate: 30]
]
def x = [list1, list2].transpose()*.sum().each { m ->
m.balance = m.quota - m.copiesToDate
}
x.each {
println it
}
demonstrates the idea and prints:
[name:Coord1, quota:200, copiesToDate:60, balance:140]
[name:Coord2, quota:300, copiesToDate:30, balance:270]
I have renamed map1 and map2 into list1 and list2 since they are in fact two lists containing inner maps.
The code is somewhat concise and might need a bit of explanation if you're not used to transpose and the groovy spread and map operations.
Explanation:
[list1, list2] - first we create a new list where the two existing lists are elements. So we now have a list of lists where the elements in the inner lists are maps.
.transpose() - we then call transpose which might need a bit of effort to grasp when you see it for the first time. If you have a list of lists, you can see transpose as flipping the lists "into the other direction".
In our case the two lists:
[[name:Coord1, quota:200], [name:Coord2, quota:300]]
[[name:Coord1, copiesToDate:60], [name:Coord2, copiesToDate:30]]
become:
[[name:Coord1, quota:200], [name:Coord1, copiesToDate:60]]
[[name:Coord2, quota:300], [name:Coord2, copiesToDate:30]]
i.e. after transpose, everything relating to Coord1 is in the first list and everything relating to Coord2 is in the second.
Each of the lists we have now is a list of Maps. But what we want is just one map for Coord1 and one map for Coord2. So for each of the above lists, we now need to coalesce or merge the contained maps into one map. We do this using the fact that in groovy map+map returns a merged map. Using the groovy spread operator *. we therefore call sum() on each list of maps.
i.e.:
[[name:Coord1, quota:200], [name:Coord1, copiesToDate:60]].sum()
computes into:
[name:Coord1, quota:200, copiesToDate:60]
and:
[[name:Coord2, quota:300], [name:Coord2, copiesToDate:30]].sum()
into:
[name:Coord2, quota:300, copiesToDate:30]
lastly we want to add the balance property to the maps so we iterate through what is now a list of two maps and add balance as a computation of quota - copiesToDate. The each construct returns the list it is working on which is what we assign to x.
Don't call find twice. Use the Map.plus() method to append new entries. Handle missing names from map2.
def map3 = map1.collect {m1 ->
def m2 = map2.find {it.name == m1.name} ?: [copiesToDate: 0]
m1 + m2 + [balance: m1.quota - m2.copiesToDate]
}
Another option for fun :-)
def result = (map1 + map2).groupBy { it.name }
.values()
*.sum()
.collect { it << ['balance': it.quota - it.copiesToDate] }
add the lists together
group by the name
get the grouped values and concatenate them
then for each of them, work out the balance

Groovy Comparing two Strings

Why these two strings are not matching on neither one: .equals() OR .compareTo() OR == OR Objects.equals()? I believe it should match atleast at one comparions.
Process proc1 = 'cat /home/output.txt'.execute()
Process proc2 = 'grep -o -m 1 webServiceReturn'.execute()
Process all = proc1 | proc2
def log = all.text.toString()
String svc = "webServiceReturn";
println (log)
println (svc)
//if (svc.equals(log)) {
//if (svc.compareTo(log)) {
//if(svc == log) {
if (Objects.equals((svc),(log))) {
println "MATCHED" }
else {
println "NOT MATCHED" }
The result on all four comparison are:
webServiceReturn
webServiceReturn
NOT MATCHED
I was able to reproduce your issue and making one minor modification to trim the white space on the results allowed it to match.
Do a trim on your all.text to remove any extraneous white space.
def log = all.text.trim()
You don't need the .toString() call either.
I just learned from another StackOverflow answer that using equals(), .contains and in fail to see same contents of strings of different type.
Groovy different results on using equals() and == on a GStringImpl
According to an answer by #dunes in the above question, the Groovy comparison using == uses compareTo() opportunistically before equals(). (The == comparison in Groovy has a remarkable difference from Java where it compares references).
def expandedString = "${'test'}"
def simpleString = 'test'
println "Expansion: ${expandedString}"
println "equals 'test' ? ${expandedString.equals(simpleString)}"
println " == 'test' ? ${expandedString == simpleString}"
The above outputs the following,
Expansion: test
equals 'test' ? false
== 'test' ? true
The following excerpt from the Groovy documentation omits the compareTo() piece that makes == different from equals().
In Groovy, using == to test equality is different from using the same operator in Java. In Groovy, it is calling equals. If you want to compare reference equality, you should use is like in the following example:
def list1 = ['Groovy 1.8','Groovy 2.0','Groovy 2.3']
def list2 = ['Groovy 1.8','Groovy 2.0','Groovy 2.3']
assert list1 == list2
assert !list1.is(list2)
http://docs.groovy-lang.org/next/html/documentation/core-operators.html#_identity_operator

Finding the key in a map, given the value

Hi I have a map like this :
[this:0, is:1, a:2, file:3, anotherkey:4, aa:5]
I wish I could find the key's given the value of a map. For example, if the value 5 is given I need to return aa from the map.
Is that possible?
I don't know if there's a direct method to get a key for a given value, but using Map#find to get a map entry and then get its value should be enough:
def keyForValue(map, value) {
map.find { it.value == value }?.key
}
def map = [a: 1, b: 2, c: 3]
assert keyForValue(map, 2) == 'b'
assert keyForValue(map, 42) == null
In general, maps don't need to have an order relation between their entries, but the default implementation for Groovy's literal maps is LinkedHashMap, which is ordered, so the keyForValue will always yield the first key for a value when using those maps.
There's no specific command for that.
Fortunately, as showed here, you can easily get the key(s) for a specific value in a map:
def myMap = [this:0, is:1, a:2, file:3, fix:4, aa:5]
def myValue = 5
You can do:
def myKey = myMap.find{ it.value == myValue }?.key
// 'aa'
If you want all the keys, do something like this:
def myMap = [this:0, is:1, a:2, file:3, fix:4, aa:5, bb:5]
def myValue = 5
def myKeys = []
myMap.findAll{ it.value == myValue }.each{myKeys << it?.key}
// ['aa', 'bb']
You could invert the map, like this:
Map m = [a: '1', b: '2']
Map mInvert = m.collectEntries { e -> [(e.value): e.key] }
assert mInvert == ['1':'a', '2':'b']
assert mInvert['2'] == 'b'
assert m['b'] == '2'
You'll probably have to iterate over the entry set yourself and try to find the entry with a matching value.
def expect = 5
def m = ['this':0, is:1, a:2, file:3, aa:5]
def r = m.collectMany{ k,v -> (v == expect) ? [k] : []}
// Result: [aa]

spread on range and map

I have a couple questions on applying spread operator on range and map. Refer to code below, error lines are marked.
(1) "assert" works on the updated range, but why doesnt "println" print it?
(2) when we say "*range" groovy can figure out and extend the range. So why doesnt "map" work also, why do we need to say ":map" ?
def range = (1..3)
println range // prints: [1,2,3]
assert [0,1,2,3] == [0,*range] // works ok
println [0, *range] // error
def map = [a:1, b:2]
assert [a:1, b:2, c:3] == [c:3, *:map] // works ok
assert [a:1, b:2, c:3] == [c:3, *map] // error
When you call:
println [0, *range]
it is trying to call getAt on a property println. You need to wrap the list in braces to help the parser out:
println( [ 0, *range ] )
And for the second error, * in this form is the spread operator. It is used to spread Lists.
You have a map, so need to use the spread map operator *: (as you have seen)

Groovy String concatenation with null checks

Is there a better way to do this? Note: part1, part2 and part3 are string variables defined elsewhere (they can be null).
def list = [part1, part2, part3]
list.removeAll([null])
def ans = list.join()
The desired result is a concatenated string with null values left out.
You can do this:
def ans = [part1, part2, part3].findAll({it != null}).join()
You might be able to shrink the closure down to just {it} depending on how your list items will evaluate according to Groovy Truth, but this should make it a bit tighter.
Note: The GDK javadocs are a great resource.
If you use findAll with no parameters. It will return every "truthful" value, so this should work:
def ans = [part1, part2, part3].findAll().join()
Notice that findAll will filter out empty strings (because they are evaluated as false in a boolean context), but that doesn't matter in this case, as the empty strings don't add anything to join() :)
If this is a simplified question and you want to keep empty string values, you can use findResults{ it }.
Alternatively, you can do this as a fold operation with inject:
def ans = [part1, part2, part3].inject('') { result, element ->
result + (element ?: '')
}
This iterates the whole list and concatenates each successive element to a result, with logic to use the empty string for null elements.
You could use grep:
groovy:000> list = ['a', 'b', null, 'c']
===> [a, b, null, c]
groovy:000> list.grep {it != null}.join()
===> abc

Resources