如何获取压缩文件(通过索引)并重新创建原始文件? (JAVA)

问题的背景

我一直在开发一些代码,首先关注的是读取字符串并创建文件。 其次,将字符串拆分为数组。 然后获取数组中每个单词的索引,最后删除重复项并将其打印到不同的文件。 我目前已经为此创建了代码,这是一个链接https://pastebin.com/gqWH0x0 (也有一个菜单系统),但它相当长,所以我没有在这个问题中实现它。

压缩方法通过哈希映射完成,获取数组的索引并将它们映射到相关的单词。 这是一个例子:

原文:“海海见海看见”

输出:见[2,4,5],海[0,1,3],

下一阶段是将输出恢复到原始状态。 我目前相对较新的java,所以我不知道所需的技术。 代码应该能够获取输出文件(如上所示)并将其放回原始文件中。

我目前的想法是你只需要重写这个hashmap(如下)。 这样想我会不正确? 我以为我应该首先检查堆栈溢出!

Map<String, Set> seaMap = new HashMap(); //new hashmap for (int seaInt = 0; seaInt < sealist.length; seaInt++) { if (seaMap.keySet().contains(sealist[seaInt])) { Set index = seaMap.get(sealist[seaInt]); index.add(seaInt); } else { Set index = new HashSet(); index.add(seaInt); seaMap.put(sealist[seaInt], index); } } System.out.print("Compressed: "); seaMap.forEach((seawords, seavalues) -> System.out.print(seawords + seavalues + ",")); System.out.println("\n"); 

如果有人有任何好的想法/答案,那么请让我知道,我真的很想要一个解决方案!

链接到当前代码: https : //pastebin.com/gqWH0x0K

首先,您必须使用您的示例将压缩行中带索引的单词分开:

 "see[2, 4, 5],sea[0, 1, 3]," 

获得以下字符串:

 "see[2, 4, 5]" and "sea[0, 1, 3]" 

对于每个人,您必须阅读索引,例如:

 2, 4 and 5 

现在只需在给定索引处的ArrayList(或数组)中写入该单词。

对于前两个步骤,您可以使用正则表达式查找每个单词和索引列表。 然后使用String.split和Integer.parseInt获取所有索引。

 Pattern pattern = Pattern.compile("(.*?)\\[(.*?)\\],"); String line = "see[2, 4, 5],sea[0, 1, 3],"; Matcher matcher = pattern.matcher(line); while (matcher.find()) { String word = matcher.group(1); String[] indexes = matcher.group(2).split(", "); for (String str : indexes) { int index = Integer.parseInt(str); 

现在只需检查结果List是否足够大,并在找到的索引处设置单词。