我们如何从String列表中删除重复元素而不考虑每个单词的大小写,例如考虑下面的代码片段
String str = "Kobe Is is The the best player In in Basketball basketball game .";
List<String> list = Arrays.asList(str.split("\\s"));
list.stream().distinct().forEach(s -> System.out.print(s+" "));
Run Code Online (Sandbox Code Playgroud)
这仍然提供与下面相同的输出,这是显而易见的
Kobe Is is The the best player In in Basketball basketball game .
Run Code Online (Sandbox Code Playgroud)
我需要如下结果
Kobe Is The best player In Basketball game .
Run Code Online (Sandbox Code Playgroud)
从字面上看你的问题,"无论列表中的情况如何都删除重复的字符串",你可以使用
// just for constructing a sample list
String str = "Kobe Is is The the best player In in Basketball basketball game .";
List<String> list = new ArrayList<>(Arrays.asList(str.split("\\s")));
// the actual operation
TreeSet<String> seen = new TreeSet<>(String.CASE_INSENSITIVE_ORDER);
list.removeIf(s -> !seen.add(s));
// just for debugging
System.out.println(String.join(" ", list));
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1696 次 |
| 最近记录: |