English 中文(简体)
绘制使用格罗莫夫的地图
原标题:Split a map using Groovy

我想把地图分为一系列地图。 例如,如果有一个有25个关键/价值乳制品的地图。 我希望每个地图上有不超过10个元素的地图。

我怎么做呢?

我没有放弃一个解决办法,即有更好的粗体:

  static def splitMap(m, count){
    if (!m) return

    def keys = m.keySet().toList()
    def result = []
    def num = Math.ceil(m?.size() / count)
    (1..num).each {
      def min = (it - 1) * count
      def max = it * count > keys.size() ? keys.size() - 1 : it * count - 1
      result[it - 1] = [:]
      keys[min..max].each {k ->
        result[it - 1][k] = m[k]
      }
    }
    result
  }

m 地图。 数量是地图内内容的最大数量。

最佳回答

Map.metaClass.partition = { size ->
  def rslt = delegate.inject( [ [:] ] ) { ret, elem ->
    ( ret.last() << elem ).size() >= size ? ret << [:] : ret
  }
  rslt.last() ? rslt : rslt[ 0..-2 ]
}

因此,如果你照此图:

def origMap = [1: a , 2: b , 3: c , 4: d , 5: e , 6: f ]

下列所有主张均通过:

assert [ [1: a ], [2: b ], [3: c ], [4: d ], [5: e ], [6: f ] ] == origMap.partition( 1 )
assert [ [1: a , 2: b ], [3: c , 4: d ], [5: e , 6: f ] ]       == origMap.partition( 2 )
assert [ [1: a , 2: b , 3: c ], [4: d , 5: e , 6: f ] ]         == origMap.partition( 3 )
assert [ [1: a , 2: b , 3: c , 4: d ], [5: e , 6: f ] ]         == origMap.partition( 4 )
assert [ [1: a , 2: b , 3: c , 4: d , 5: e ], [6: f ] ]         == origMap.partition( 5 )
assert [ [1: a , 2: b , 3: c , 4: d , 5: e , 6: f ] ]           == origMap.partition( 6 )

或作为<代码>Category (为避免在<代码>Map上添加任何内容:

class MapPartition {
  static List partition( Map delegate, int size ) {
    def rslt = delegate.inject( [ [:] ] ) { ret, elem ->
      ( ret.last() << elem ).size() >= size ? ret << [:] : ret
    }
    rslt.last() ? rslt : rslt[ 0..-2 ]
  }
}

然后,如果你需要这一功能,你可以简单地读到<条码>。 a. 同类:

use( MapPartition ) {
  assert [ [1: a ], [2: b ], [3: c ], [4: d ], [5: e ], [6: f ] ] == origMap.partition( 1 )
  assert [ [1: a , 2: b ], [3: c , 4: d ], [5: e , 6: f ] ]       == origMap.partition( 2 )
  assert [ [1: a , 2: b , 3: c ], [4: d , 5: e , 6: f ] ]         == origMap.partition( 3 )
  assert [ [1: a , 2: b , 3: c , 4: d ], [5: e , 6: f ] ]         == origMap.partition( 4 )
  assert [ [1: a , 2: b , 3: c , 4: d , 5: e ], [6: f ] ]         == origMap.partition( 5 )
  assert [ [1: a , 2: b , 3: c , 4: d , 5: e , 6: f ] ]           == origMap.partition( 6 )
}
问题回答

暂无回答




相关问题
Groovy - how to exit each loop?

I m new to Grails/Groovy and am trying to find a node in a an xml file; I ve figured out how to iterate over all of them, but I want to exit the loop when the target node is found. I ve read that ...

Eclipse Spring Builder set properties with Groovy beans

I typically use groovy to construct simple bean but the Spring IDE plugin to eclipse fails to build when I try to set a property that is generated by groovy without an explicit setter. For example, ...

How can I get this snippet to work?

I d like to port a little piece of code from Ruby to Groovy, and I m stuck at this: def given(array,closure) { closure.delegate = array closure() } given([1,2,3,4]) { findAll { it > 4} ...

Changing the value in a map in Groovy

This is about a very basic program I m writing in Groovy. I have defined a map inside a method: def addItem() { print("Enter the item name: ") def itemName = reader.readLine() print(...

Is functional Clojure or imperative Groovy more readable?

OK, no cheating now. No, really, take a minute or two and try this out. What does "positions" do? Edit: simplified according to cgrand s suggestion. (defn redux [[current next] flag] [(if flag ...

热门标签