Created
December 5, 2013 18:15
-
-
Save jvmvik/7810428 to your computer and use it in GitHub Desktop.
Algorithm in Groovy to create union and exclusion, and intersection between map and list.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Experiment | |
{ | |
/** | |
* Experiment algorithm in Groovy | |
* to create union and exclusion, | |
* intersection between map and list. | |
*/ | |
@Test | |
void code() | |
{ | |
Map m1 = [a:1, b:3, c:0] // new | |
Map m2 = [a:2, b:3, d:0] // old | |
println(m1) | |
println(m2) | |
println('----') | |
println("common") | |
println m1.intersect(m2) | |
assert [b:3] == m1.intersect(m2) | |
def mi = m1.intersect(m2) | |
if(mi.size() == m1 && mi.size() == m2) | |
fail('not equals') | |
else | |
{ | |
def m1keys = [] | |
m1keys.addAll(m1.keySet()) | |
m1keys.removeAll(mi.keySet()) | |
println "changed" | |
println m1keys.intersect(m2.keySet()) | |
assert m1keys.intersect(m2.keySet()).toString() == '[a]' | |
println "new" | |
m1keys.removeAll(m2.keySet()) | |
println m1keys | |
assert m1keys.toString() == '[c]' | |
println "deleted" | |
def m2keys = [] | |
m2keys.addAll(m2.keySet()) | |
m2keys.removeAll(m1.keySet()) | |
println(m2keys) | |
assert ['d'] == m2keys | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment