如何从文本文件中删除重复的行
刚刚接触java! 我想删除一个文件中的重复行,但我需要保持线,更数的大写字母如何从文本文件中删除重复的行
,可以使用哪些功能,如果文本文件包含下列单词来实现这一
输入
汽车
车
的BU
总线
自行车
预期输出
车
的BU
自行车
试试这个。
static class Str {
final String origin;
final int uppers;
Str(String origin) {
this.origin = origin;
this.uppers = (int)origin.chars()
.filter(Character::isUpperCase)
.count();
}
}
public static List<String> uniq(String file) throws IOException {
Path path = Paths.get(file);
List<String> lines = Files.readAllLines(path);
Map<String, Str> map = new LinkedHashMap<>();
for (String e : lines) {
Str n = new Str(e);
map.compute(e.toLowerCase(),
(k, v) -> v == null || n.uppers > v.uppers ? n : v);
}
return map.values().stream()
.map(s -> s.origin)
.collect(Collectors.toList());
}
和
System.out.println(uniq("test.txt"));
结果:
[CaR, BUs, bike]
Pefect回答正在寻找什么 – Gpn008
你可以先阅读文本文件和文本的每一行存储串
的列表,然后你可以使用
str1.equalsIgnoreCase(STR2);
来比较字符串
如果返回true,就忽略这个文本
“_,但我需要保留更多数量的大写字母的行_”... –
但我需要保留更多数量的大写字母(您从来没有在原始问题中提及此行)的行数是什么意思这就是我告诉你根据你的问题尝试,如果你不明白英语,那么你的问题是正确的,然后告诉我() –
1)我没有提出这个问题 - 我不知道你为什么要像我一样对我说话。 2)原始问题包含此确切的行 - 请参阅http://stackoverflow.com/revisions/39639481/1 –
[在使用Java文件删除重复的行]的可能的复制(http://stackoverflow.com/questions/996041/deleting -duplicate-in-a-file-using-java) – DimaSan
@DimaSan这不仅仅是在你的链接中回答 - “_,但我需要保留更多数量的大写字母_”。 –