Can you help me identify what is wrong in the simple remove from the map below (notice output right below it)?
public class Issue {
...
def allButThis() {
println "allButThis ..."
def all = Issue.list()
println "all is ${all}"
all.remove(this.id)
println "all with ${this.id} removed: ${all}"
return all
}
String toString() {return "${id}: ${title}"}
When I run this, I get the following results, i.e. item 2 is not removed as expected
allButThis ...
all is [1: Issue-1, 2: Issue-2, 3: Issue-3]
all with 2 removed: [1: Issue-1, 2: Issue-2, 3: Issue-3]
As far as I can tell, this remove should work, per e.g. http://groovy.codehaus.org/JN1035-Maps, where the “remove” is described one third down the page.
I’m using Grails 1.3.7.
Thanks
P.S. I added my toString() method (above), perhaps I fooled myself.
————- update ———-
I removed my toString() method, and followed Rob’s solution, namely:
all.remove(this)
println "all with ${this} removed: ${all}"
which produces:
all is [momentum.Issue : 1, momentum.Issue : 2, momentum.Issue : 3]
all with momentum.Issue : 2 removed: [momentum.Issue : 1, momentum.Issue : 3]
this.idis of typeLongand therefore, if you want to remove the n’th item, you need to convert it to an integer. however this is highly dangerous since the id’s entry is not always the n’th entry. remove this item using.findAll({it.id != this.id})would be more save.In this case, I would recommend you to do it like this: