【问题标题】:Datatypes in groovy similar to java?groovy中的数据类型类似于java?
【发布时间】:2014-02-26 10:38:15
【问题描述】:

我想弄清楚我应该在 java 中使用什么类似于 groovy 的类似数据类型?

我的 groovy 代码如下

statistic = [:]
entries.each {
    minute = it.getRequestTime()

    value = it.getValue()

    if (statistic[minute] == null) {
        statistic[minute] = [:]
    }

    if (statistic[minute][value] == null) {
        statistic[minute][value] = ['count': 0L, 'timesum': 0L, 'min': Long.MAX_VALUE, 'max': Long.MIN_VALUE]
    }
}

我是 groovy 的新手,很难理解他们使用的数据类型。我可以理解他们在 groovy 中使用了 Hashmap (statistic = [:])。但后者对我来说真的很困惑。谁能帮我理解这段代码?

【问题讨论】:

    标签: java types groovy


    【解决方案1】:

    这些是嵌套地图,地图中的地图。

    if (statistic[minute] == null) 
    

    这意味着,如果minute 键没有条目,则在此处放置一个新的空映射。

    if (statistic[minute][value] == null)
    

    这意味着,如果statistic[minute] 中没有条目
    对于密钥value,然后在此处放置另一张地图。

    【讨论】:

    • 最后一次分配统计是否是这样的Map<Map<String,Map<String,String>>,Long>[分钟][值] = ['count': 0L, 'timesum': 0L, 'min': Long.MAX_VALUE, ' max': Long.MIN_VALUE]?
    • 是的,他们将地图放入 statistic[minute][value]。
    • 假设分钟和值是字符串,它更像Map<String,Map<String,Map<String,Long>>>
    猜你喜欢
    • 2015-01-03
    • 2023-02-23
    • 1970-01-01
    • 2015-08-15
    • 1970-01-01
    • 2021-06-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多