【发布时间】:2011-12-06 21:38:11
【问题描述】:
从集合fileMatches,我想将具有10 个最大值的地图分配给一个名为topTen 的新集合。所以我尝试做一个收藏:
def fileMatches = [:].withDefault{[]}
new File('C:\\BRUCE\\ForensicAll.txt').eachLine { line ->
def (source, matches) = line.split (/\t/)[0, 2]
fileMatches[source] << (matches as int)
我想遍历我的收藏并获取 10 个具有最大价值的地图。我可能遇到的一个问题是它的输出看起来不像我想象的那样。例如一个条目:
C:\cygwin\home\pro-services\git\projectdb\project\stats\top.h:[984, 984]
到目前为止的建议非常好,但我不确定我的收藏是否被安排为利用建议的解决方案(我有 filename:[984, 984] 当我可能想要 [filename, 984] 作为我收藏中的地图条目时) .我还不太明白这些东西(比如fileMatches[source] << (matches as int) 是如何工作的,因为它会产生我在上面发布的行(source:[matches, matches] 是输出)。
请指教,感谢您的帮助!
【问题讨论】:
-
所以直截了当地说:
fileMatches是一个地图列表,每个地图的形式为[someFilename: value]?并且您想检索fileMatches中具有someFilename最大值的10 个条目? -
好吧,我不确定(这里是新手)。使用这段代码:
def fileMatches = [:].withDefault{[]} new File('C:\\documents\\ForensicAll.txt').eachLine { line -> def (source, matches) = line.split (/\t/)[0, 2] fileMatches[source] << (matches as int)我最终得到了一堆看起来像这样的东西,这可能不是我想要的C:\programs\xwing\lasers.h:[25, 25] -
对不起,但应该澄清你的问题以及你的代码示例。我们很困惑。向我们展示给定的输入和预期的输出。
-
好的,对不起,我试着把它清理了:)
-
我发了一篇新帖子来解决我的收藏集的定义stackoverflow.com/questions/8408556/…
标签: arrays list loops collections groovy