我想将对象存储在地图中(称为结果)。从SQL行创建或更新对象。 对于我读过的每一行,我按如下方式访问地图:
def result = [:]
sql.eachRow('SELECT something') { row->
{
// check if the Entry is already existing
def theEntry = result[row.KEY]
if (theEntry == null) {
// create the entry
theEntry = new Entry(row.VALUE1, row.VALUE2)
// put the entry in the result map
result[row.KEY] = theEntry
}
// use the Entry (create or update the next hierarchie elements)
}
我想最小化检查和更新地图的代码。如何才能做到这一点?
我知道函数map.get(key, defaultValue)
,但我不会使用它,因为即使我不需要它,在每次迭代时创建一个实例也很昂贵。
我想要的是一个带有闭包的get函数,用于提供默认值。在这种情况下,我会进行懒惰评估。
更新
dmahapatro提供的解决方案正是我想要的。下面是一个使用示例。
// simulate the result from the select
def select = [[a:1, b:2, c:3], [a:1, b:5, c:6], [a:2, b:2, c:4], [a:2, b:3, c:5]]
// a sample class for building an object hierarchie
class Master {
int a
List<Detail> subs = []
String toString() { "Master(a:$a, subs:$subs)" }
}
// a sample class for building an object hierarchie
class Detail {
int b
int c
String toString() { "Detail(b:$b, c:$c)" }
}
// the goal is to build a tree from the SQL result with Master and Detail entries
// and store it in this map
def result = [:]
// iterate over the select, row is visible inside the closure
select.each { row ->
// provide a wrapper with a default value in a closure and get the key
// if it is not available then the closure is executed to create the object
// and put it in the result map -> much compacter than in my question
def theResult = result.withDefault {
new Master(a: row.a)
}.get(row.a)
// process the further columns
theResult.subs.add new Detail(b: row.b, c: row.c )
}
// result should be [
// 1:Master(a:1, subs:[Detail(b:2, c:3), Detail(b:5, c:6)]),
// 2:Master(a:2, subs:[Detail(b:2, c:4), Detail(b:3, c:5)])]
println result
我从这个样本中学到了什么:
答案 0 :(得分:8)
你问过它,Groovy为你准备好了。 :)
def map = [:]
def decoratedMap = map.withDefault{
new Entry()
}
它的工作方式与您期望它懒惰地工作的方式相同。有关详细说明,请查看withDefault API。