使用 Stream API 对字符串进行排序
使用 Stream.sorted()
API 对字符串的字符进行排序的示例:
String randomString = "adcbgekhs"; String sortedChars = Stream.of( randomString.split("") ) .sorted() .collect(Collectors.joining()); System.out.println(sortedChars); // abcdeghks
Stream.sorted()
和 Arrays.sort()
方法按字母顺序对字符串的字符进行排序查看更多教程 https://on itroad.com
使用 Arrays.sort()
String randomString = "adcbgekhs"; // Java将字符串转换为字符数组 char[] chars = randomString.toCharArray(); // 排序字符数组 Arrays.sort(chars); // 将字符数组转换回 字符串 String sortedString = String.valueOf(chars); System.out.println(sortedChars); // abcdeghks
日期:2020-09-17 00:09:44 来源:oir作者:oir