...... 一个只会用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]]
如果想学习,可以看别人的源码学习思维,想我只需要能用就行,哈哈