For example, enter "abc" to print all possible combinations and eliminate duplicate values.
The so-called arrangement combination is as follows:
Arrangement combination, string:abc
bca
acb
abc
cba
bac
cab
Number of arrangement and combinations: 6
Implementation code (combined with Java8 lambda expression implementation)
import org.junit.Test;import java.util.ArrayList;import java.util.HashSet;import java.util.List;public class test2 { @Test public void test3() { String input="abc"; //1. Start arranging List<String> sortResult = sort(input); System.out.println("Sort combination, string:"+input); //2. Eliminate duplicate columns HashSet h = new HashSet(sortResult); sortResult.clear(); sortResult.addAll(h); //3.Printout sortResult.forEach(e -> System.out.println(e)); //4.Print number of printing System.out.println("Arrange number of combinations: " + sortResult.size()); } private List<String> sort(String input) { List<String> sortList = new ArrayList(); if (input == null || "".equals(input)) { System.out.println("Tip: You have entered a null character, please enter a valid value!"); return new ArrayList(); } char leftChar = input.charAt(0); if (input.length() > 1) { String rightString = input.substring(1, input.length()); List<String> rightStringSortedList = sort(rightString); rightStringSortedList.forEach((e) -> { for (int i = 0; i < e.length() + 1; i++) { sortList.add(new StringBuffer(e).insert(i, leftChar).toString()); } }); } else { sortList.add(String.valueOf(leftChar)); } return sortList; }}If there is a more concise code implementation, please do not be stingy and post it and share it.
The above is all the content of this article. I hope it will be helpful to everyone's learning and I hope everyone will support Wulin.com more.