import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;
public class Pailiezuhe {
public static void main(String[] args) {
List<String> list = Arrays.asList("abcd".split(""));
System.out.println(permutationNoRepeat(list, 2));
}
//list是返回的结果集,length是要组合的字符串长度
public static List<String> permutationNoRepeat(List<String> list, int length) {
Stream<String> stream = list.stream();
for (int i = 1; i < length; i++) {
stream = stream.flatMap(s -> list.stream().filter(aChar -> !s.contains(aChar)).map(aChar -> s.concat(aChar)));
}
return stream.collect(Collectors.toList());
}
}
运行结果
//length=2
[ab, ac, ad, ba, bc, bd, ca, cb, cd, da, db, dc]
//length=3
[abc, abd, acb, acd, adb, adc, bac, bad, bca, bcd, bda, bdc, cab, cad, cba, cbd, cda, cdb, dab, dac, dba, dbc, dca, dcb]