Java 删除以连字符分隔的大字符串中的重复字符串集

Java 删除以连字符分隔的大字符串中的重复字符串集,java,Java,我正在用Java开发,我有以下字符串: String duplicates = "Smith, John - Smith, John - Smith, John – Wilson, Peter"; 我需要得到一个没有重复名称的新字符串 unique = "Smith, John – Wilson, Peter"; 我想我可以用 String unique[] = duplicates.split("-"); 用逗号拆分连字符的问题是,现在我有了所有的逗号 Smith, John, Smit

我正在用Java开发,我有以下字符串:

String duplicates = "Smith, John - Smith, John - Smith, John – Wilson, Peter";
我需要得到一个没有重复名称的新字符串

unique = "Smith, John – Wilson, Peter";
我想我可以用

String unique[] = duplicates.split("-");
用逗号拆分连字符的问题是,现在我有了所有的逗号

Smith, John, Smith, John, Smith, John, Wilson, Peter
任何帮助都将不胜感激

您可以使用
distinct()
操作流

Arrays.stream(duplicates.split("\\s+(-|–|‒|–|—|―)+\\s+")) // split by different types of dashes surrounded by whitespaces
      .distinct()        // get rid of duplicates
      .collect(Collectors.toList())
      .forEach(System.out::println); // print each entry
输出:

Smith, John
Wilson, Peter
或者使用
收集器。加入
以获取不重复的字符串:

String duplicates = "Smith, John -- Smith, John - Smith, John – Wilson, Peter ‒ Yves Saint-Laurent ― George Henry Lane-Fox Pitt-Rivers";

String noDuplicates = Arrays.stream(duplicates.split("\\s+(-|–|‒|–|—|―)+\\s+"))
                            .distinct()
                            .collect(Collectors.joining(" – "));
System.out.println(noDuplicates);
印刷品:

Smith, John – Wilson, Peter – Yves Saint-Laurent – George Henry Lane-Fox Pitt-Rivers
我更新了对名称的检测,这些名称可能包含单连字符,以处理非常流行的“双筒”名称,并添加了类型

您可以使用流的
distinct()
操作

Arrays.stream(duplicates.split("\\s+(-|–|‒|–|—|―)+\\s+")) // split by different types of dashes surrounded by whitespaces
      .distinct()        // get rid of duplicates
      .collect(Collectors.toList())
      .forEach(System.out::println); // print each entry
输出:

Smith, John
Wilson, Peter
或者使用
收集器。加入
以获取不重复的字符串:

String duplicates = "Smith, John -- Smith, John - Smith, John – Wilson, Peter ‒ Yves Saint-Laurent ― George Henry Lane-Fox Pitt-Rivers";

String noDuplicates = Arrays.stream(duplicates.split("\\s+(-|–|‒|–|—|―)+\\s+"))
                            .distinct()
                            .collect(Collectors.joining(" – "));
System.out.println(noDuplicates);
印刷品:

Smith, John – Wilson, Peter – Yves Saint-Laurent – George Henry Lane-Fox Pitt-Rivers


我更新了对可能包含单连字符的名称的检测,以处理非常流行的“双筒”名称,并添加了类型

我认为您需要在逗号而不是破折号上拆分?是的,我知道您的意思,但是,有没有办法解决连字符的问题?这些名称不是完整的“Smith,John”吗?在这种情况下,您通过执行拆分('-')来获得正确的名称?例如,下面将打印唯一的名称:
String str=“Smith,John-Smith,John-Smith,John-Wilson,Peter”
Arrays.stream(str.split(“-”).map(s->s.trim()).distinct().forEach(System.out::println)这是否回答了您的问题?我想你需要用逗号而不是破折号来分开?是的,我知道你的意思,但是,有没有办法解决连字符的问题?名字不是完整的“Smith,John”吗?在这种情况下,您通过执行拆分('-')来获得正确的名称?例如,下面将打印唯一的名称:
String str=“Smith,John-Smith,John-Smith,John-Wilson,Peter”
Arrays.stream(str.split(“-”).map(s->s.trim()).distinct().forEach(System.out::println)这是否回答了您的问题?我有一个简短的问题。。。。为什么使用(“-|-”)?如果输入字符串包含不同类型的破折号/连字符,则至少有两种类型:
“-”
“-”
。您可以在我的示例中检查更新的输入字符串。是否关闭它?不知道这是什么意思?你可以接受我的回答——打绿色tick@Guisselle您可能对接受回答机制感兴趣。请看,我有一个简短的问题。。。。为什么使用(“-|-”)?如果输入字符串包含不同类型的破折号/连字符,则至少有两种类型:
“-”
“-”
。您可以在我的示例中检查更新的输入字符串。是否关闭它?不知道这是什么意思?你可以接受我的回答——打绿色tick@Guisselle您可能对接受回答机制感兴趣。请看