Collections Java String Handling

Remove duplicate words from String in Java example

Remove duplicate words from String in Java example shows how to remove duplicate words from String in Java. Example also shows various approaches to remove duplicate words from String in Java.

How to remove duplicate words from String in Java?

Consider below given string value.

Below given example shows how to remove duplicate words from String.

Output

Since our string contained words separated by a space, we first split the string by one or more space. Once we had all the words in form of String array, we converted the String array to LinkedHashSet using asList method of Arrays class. Since Set does not allow duplicate elements, duplicate words were not added to the LinkedHashSet. Once we had all the unique words in LinkedHashSet, we joined them with space to create a string without duplicate words.

As we have seen the longer and descriptive version of the program above, it’s time for the shortcut. Above given complete program can be rewritten in just one line.

Output

Just one line. We clubbed creation of LinkedHashSet from the List of words which is created from splitting the string by space. Instead of iterating the Set, we used toString method which returns the set elements in “[e1, e2, e3, ..]” format. Since “[“, “]” and “,” were not needed, we replaced them with empty string.

How to remove duplicate words from String using Java 8?

If you are using Java 8, you can do that in just one line of code as given below.

Output

Final words: Why we used LinkedHashSet and not HashSet? Well, the simple reason is HashSet does not maintain the order of the elements. So if we used HashSet to store the words, we could not guarantee the order of the words in sentence once we joined them back using space. LinkedHashSet maintains the order so after removing duplicates, we got the words in same order.

Please let us know your views in the comments section below.

 

Join 1000+ fellow learners! Enter your email address below: