java实现排列组合工具

...... 一个只会用java的男人

我们业务有时需要实现排列组合的需求,今天为大家推荐一款比较好用的排列组合工具包,github开源工具。

github地址: https://github.com/dpaukov/combinatoricslib3

我这里只举一个简单的例子,其余github上有对应的例子,可以根据自身需要学习,实现结果都差不多。

比如我们有1~5的集合,我们需要得到两两元素的组合方式,我们见证奇迹的时候:

<dependency>
    <groupId>com.github.dpaukov</groupId>
    <artifactId>combinatoricslib3</artifactId>
    <version>3.3.0</version>
</dependency>
List<Integer> list = Stream.of(1, 2, 3, 4, 5).collect(Collectors.toList());
List<List<Integer>> combination = combination(list, 2);
System.out.println(combination);
public static <T> List<List<T>> combination(List<T> list,Integer length){
    return Generator.combination(list)
            .simple(length)
            .stream()
            .collect(Collectors.toList());
}

输出结果为:[[1, 2], [1, 3], [1, 4], [1, 5], [2, 3], [2, 4], [2, 5], [3, 4], [3, 5], [4, 5]]

如果想学习,可以看别人的源码学习思维,想我只需要能用就行,哈哈

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值