Remove duplicate set of strings in a hyphen separated big string

Viewed 133

I'm developing in Java, I have the following string:

String duplicates = "Smith, John - Smith, John - Smith, John – Wilson, Peter";

I need to get a new string with no duplicate names.

unique = "Smith, John – Wilson, Peter";

I thought I could use

String unique[] = duplicates.split("-");

Problem with splitting hyphens with commas is that now I have all commas

Smith, John, Smith, John, Smith, John, Wilson, Peter

Any help will be greatly appreciated

1 Answers

You can use distinct() operation of stream

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

Output:

Smith, John
Wilson, Peter

Or use Collectors.joining to get a string without duplicates:

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);

prints:

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

I updated the detection of the names which can contain single hyphens to handle "double-barrelled" names which are quite popular, and added types of dashes

Related