根据Groovy中的模式在groovy中混合两个列表?

Ath*_*hri 4 algorithm collections groovy

我有两个列表可以说:

def list1 = [a,b,c,d,e...]
def list2 = [1,2,3,4,5... ]
Run Code Online (Sandbox Code Playgroud)

我希望它们以某种模式混合,以便最终列表如下所示: [a,b,c,d,1,2,e,f,g,h,3,4,i,j,k,l,5,6...]

基本上在nlist1的每个元素之后,我m从list2 获取元素.

编辑:如果一个列表用完了元素,则剩余列表中的项目应该只是添加到最终列表中.

EDIT2:两个列表都可以将对象作为元素.

我想找到解决这个问题的最有效方法.

tim*_*tes 5

这是在Groovy中执行此操作的一种方法:

所以我有一个方法mix,它采用带有整数键的映射(所需元素的数量),并将列表作为值:

List mix( Map<Integer,List> amounts ) {
  amounts.collect { k, v ->
    v.collate( k )
  }.transpose().flatten()
}
Run Code Online (Sandbox Code Playgroud)

然后,给出:

// The letters a to z
def list1 = 'a'..'z'

// The numbers 1 to 10
def list2 = 1..10

// Call, and ask for 4 of list1 followed by 2 of list2
mix( [ 4:list1, 2:list2 ] )
Run Code Online (Sandbox Code Playgroud)

返回:

[ 'a', 'b', 'c', 'd', 1, 2,
  'e', 'f', 'g', 'h', 3, 4,
  'i', 'j', 'k', 'l', 5, 6,
  'm', 'n', 'o', 'p', 7, 8,
  'q', 'r', 's', 't', 9, 10 ]
Run Code Online (Sandbox Code Playgroud)

(格式化为在这里看起来更好)

正如您所看到的,它首先用尽了数字,当它出现时,列表结束.这是因为当一个列表用完元素时,转置会停止.

编辑:

使用Iterators进行另一种方式(因此它很懒,并且不会消耗比其他方式需要的更多内存):

class MixingIterator<T> implements Iterator<T> {
  private int idx = 0
  private List<Iterator> iter
  private List<Integer>  amts

  MixingIterator( List<List> lists, List<Integer> amounts ) {
    iter = lists*.iterator()
    int i = 0
    amts = amounts.collectMany { [ i++ ] * it }
    // OR FOR GROOVY 1.7.8
    // amts = amounts.collect { [ i++ ] * it }.flatten()
  }

  private void moveIdx() {
    idx = ++idx % amts.size()
  }

  @Override boolean hasNext() {
    iter*.hasNext().any()
  }

  @Override T next() {
    if( !hasNext() ) { throw new NoSuchElementException() }
    while( !iter[ amts[ idx ] ].hasNext() ) { moveIdx() }
    T ret = iter[ amts[ idx ] ].next()
    moveIdx()
    ret
  }

  @Override void remove() {
    throw new UnsupportedOperationException()
  }
}
Run Code Online (Sandbox Code Playgroud)

你叫它:

def list1 = 'a'..'z'
def list2 = 1..10

def ret = new MixingIterator( [ list1, list2 ], [ 4, 2 ] ).collect()
// OR FOR GROOVY 1.7.8
// def ret = new MixingIterator( [ list1, list2 ], [ 4, 2 ] ).collect { it }
Run Code Online (Sandbox Code Playgroud)

然后ret将等于:

['a', 'b', 'c', 'd', 1, 2,
 'e', 'f', 'g', 'h', 3, 4,
 'i', 'j', 'k', 'l', 5, 6,
 'm', 'n', 'o', 'p', 7, 8,
 'q', 'r', 's', 't', 9, 10,
 'u', 'v', 'w', 'x', 'y', 'z']
Run Code Online (Sandbox Code Playgroud)