
本文介绍如何利用字符及其在原始字符串中的位置信息,通过 Java 集合和流操作,高效地重建原始字符串。文章详细讲解了实现步骤,包括创建辅助类型、排序字符位置、处理空格以及使用 Collectors.joining() 方法生成最终字符串,并提供完整的代码示例。
假设我们已经将一个字符串分割成单个字符,并记录了每个字符在原始字符串中的位置索引。我们的目标是利用这些信息,包括字符及其位置的映射关系,重新构建出原始的字符串,包括原始字符串中的空格。
解决此问题的核心在于利用 Java 集合和流 API,将字符位置信息进行排序,并正确地将字符放置回其原始位置。以下是详细的步骤和代码示例:
1. 定义辅助类型 CharPosition
立即学习“Java免费学习笔记(深入)”;
为了方便处理字符及其位置信息,我们定义一个辅助类型 CharPosition,用于存储字符和其对应的位置索引。这里使用 Java 16 的 record 类型,可以简洁地表示数据类。
public record CharPosition(String ch, int pos) {}2. 创建 CharPosition 列表并排序
首先,我们需要将字符及其位置的映射关系转换为 CharPosition 对象的列表。然后,按照位置索引对列表进行排序。
import java.util.*;
import java.util.stream.Collectors;
import java.util.Arrays;
import java.util.Objects;
public class StringReconstruction {
private static final String text = "Hello Word!";
static Map<String, List<Integer>> charsIndex = new HashMap<>();
static void charsIndex() {
List<Character> charsList = text
.chars()
.mapToObj(e -> (char) e)
.toList();
System.out.println(charsList);
int position = 0;
for (Character c : charsList) {
if(!c.toString().isBlank()){
charsIndex.computeIfAbsent(c.toString(),
addCharPosition -> new ArrayList<>()).add(position);
}
position += 1;
}
System.out.println(charsIndex);
}
public record CharPosition(String ch, int pos) {}
static void charsToString() {
List<CharPosition> charPositions = charsIndex.entrySet().stream()
.flatMap(entry -> entry.getValue().stream()
.map(pos -> new CharPosition(entry.getKey(), pos))
)
.sorted(Comparator.comparingInt(CharPosition::pos))
.toList();
int wordLen = charPositions.get(charPositions.size() - 1).pos() + 1;
String[] word = new String[wordLen];
charPositions.forEach(c -> word[c.pos()] = c.ch());
String result = Arrays.stream(word)
.map(str -> Objects.requireNonNullElse(str, " "))
.collect(Collectors.joining());
System.out.println(result);
}
public static void main(String[] args) {
charsIndex();
charsToString();
}
}3. 创建字符数组并填充
创建一个足够长的字符串数组,长度为最大索引值加 1。然后,遍历 CharPosition 列表,将字符放置到数组中对应的位置。
4. 处理空格
由于原始字符串中可能包含空格,我们需要在数组中将没有字符的位置填充为空格。可以使用 Objects.requireNonNullElse 方法,将数组中的 null 值替换为空格。
5. 使用 Collectors.joining() 重建字符串
最后,使用 Collectors.joining() 方法将数组中的字符连接起来,生成最终的字符串。
完整代码示例:
import java.util.*;
import java.util.stream.Collectors;
import java.util.Arrays;
import java.util.Objects;
public class StringReconstruction {
private static final String text = "Hello Word!";
static Map<String, List<Integer>> charsIndex = new HashMap<>();
static void charsIndex() {
List<Character> charsList = text
.chars()
.mapToObj(e -> (char) e)
.toList();
System.out.println(charsList);
int position = 0;
for (Character c : charsList) {
if(!c.toString().isBlank()){
charsIndex.computeIfAbsent(c.toString(),
addCharPosition -> new ArrayList<>()).add(position);
}
position += 1;
}
System.out.println(charsIndex);
}
public record CharPosition(String ch, int pos) {}
static void charsToString() {
List<CharPosition> charPositions = charsIndex.entrySet().stream()
.flatMap(entry -> entry.getValue().stream()
.map(pos -> new CharPosition(entry.getKey(), pos))
)
.sorted(Comparator.comparingInt(CharPosition::pos))
.toList();
int wordLen = charPositions.get(charPositions.size() - 1).pos() + 1;
String[] word = new String[wordLen];
charPositions.forEach(c -> word[c.pos()] = c.ch());
String result = Arrays.stream(word)
.map(str -> Objects.requireNonNullElse(str, " "))
.collect(Collectors.joining());
System.out.println(result);
}
public static void main(String[] args) {
charsIndex();
charsToString();
}
}注意事项:
总结:
通过使用 Java 集合和流 API,我们可以有效地利用字符及其位置信息重建字符串。这种方法不仅简洁高效,而且易于理解和维护。通过定义辅助类型 CharPosition,排序字符位置,处理空格以及使用 Collectors.joining() 方法,我们可以轻松地实现字符串的重建。
以上就是从字符及其位置映射重建字符串:Java教程的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号