加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

Groovy中的真正组合

发布时间:2020-12-14 16:29:09 所属栏目:大数据 来源:网络整理
导读:是否有方法或一些智能方法易于阅读,以便在Groovy中生成 combination个元素?我知道Iterable#组合或GroovyCollections#组合,但是到目前为止我对它进行了重复的部分排列.见例子. // Groovy combinations resultdef e = ['a','b','c']def result = [e,e].combin
是否有方法或一些智能方法易于阅读,以便在Groovy中生成 combination个元素?我知道Iterable#组合或GroovyCollections#组合,但是到目前为止我对它进行了重复的部分排列.见例子.

// Groovy combinations result
def e = ['a','b','c']
def result = [e,e].combinations()
assert [['a','a'],['b',['c',['a','b'],'c'],'c']] == result

// What I'm looking for
def e = ['a','c']
def result = ???
assert [['a','c']] == result

Feel free to post alternate solutions. I’m still looking for better readability (it’s used in script for non-developers) and performance (w/o unnecessary iterations).

解决方法

我不太确定可读性,但这应该可以解决问题.

def e = ['a',e].combinations().findAll { a,b ->
    a < b
}

assert [['a','c']] == result

请注意,如果元素在列表中出现两次,则其组合也会出现两次.如果不需要,最后添加’.unique()’

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读