直接上代码:
//测试字符串
String pwd = "123456abc";
//将字符串拆分成字符数组
char[] chars = pwd.toCharArray();
List<Character> characters = new ArrayList<>();
//将字符存入到集合中
for (char aChar : chars) {
characters.add(aChar);
}
//随机乱序排列
String ordernessPassword = characters.stream()
//拉姆达表达式,随机返回排序结果
.sorted((o1, o2) -> new Integer[]{-1, 0, 1}[new Random().nextInt(3)])
//拉姆达表达式,拼接回字符串
.map(c -> c + "")
.reduce((v1, v2) -> v1 + v2)
.get();
System.out.println(ordernessPassword);