Pages

Footer Pages

Spring Boot

Java String API

Java Conversions

Kotlin Programs

Kotlin Conversions

Java Threads Tutorial

Java 8 Tutorial

Tuesday, November 9, 2021

Java 8 - Remove Duplicate Characters From String

1. Overview

In this tutorial, We'll learn how to remove all duplicate characters from the string in java and new java 8 stream api.

This problem can be solved in many ways but we focus on 3 important solutions.

Java 8 - Remove Duplicate Characters From String



2. Java Remove Duplicate Characters From String - StringBuilder


We use the StringBuilder to solve this problem. First, take the each character from the original string and add it to the string builder using append() method. Further, when adding the next character than use indexOf() method on the string builder to check if this char is already present in the string builder. If it is already present then we will not add it again to the string builder.

The same process is repeated for all the chars of original string. At final, string builder will have only the uniques values.

Look at the below code.
package com.javaprogramto.programs.strings.remove.duplicates;

public class StringRemoveDuplicatesExample1 {

	public static void main(String[] args) {

		String orignalString = "world world";

		StringBuilder builder = new StringBuilder();

		for (int i = 0; i < orignalString.length(); i++) {

			if (builder.indexOf(String.valueOf(orignalString.charAt(i))) == -1) {

				builder.append(orignalString.charAt(i));

			}
		}

		System.out.println("Original String : " + orignalString);
		System.out.println("After removing the duplicates : " + builder.toString());

	}

}

Output:
Original String : world world
After removing the duplicates : world 

From the output, you can observe that input has world word twice but the output is world once. So, duplicate chars are deleted. This code will work for any kind of input strings.

In the above program we used charAt() method, instead you can use toCharArray() method also to run the for loop.


3. Java Remove Duplicate Characters From String - HashSet


Next, we use the collection api HashSet class and each char is added to it using HashSet add() method.
add() method returns false if the char is ready present in the HashSet.

The same process is repeated till the last char of the string. So, at the end StringBuilder contains only distinct values.

Look at the below example code.
package com.javaprogramto.programs.strings.remove.duplicates;

import java.util.HashSet;
import java.util.Set;

public class StringRemoveDuplicatesExample2 {

	public static void main(String[] args) {

		String orignalString = "world world";

		StringBuilder builder = new StringBuilder();
		Set<Character> set = new HashSet<>();

		char[] chars = orignalString.toCharArray();

		for (char ch : chars) {

			if (set.add(ch)) {
				builder.append(ch);
			}
		}
		System.out.println("Original String : " + orignalString);
		System.out.println("After removing the duplicates : " + builder.toString());

	}

}

Output is same as above section.

4. Java 8 Streams - Remove Duplicate Characters From String


Finally, apply the java 8 functional style to eliminate the duplicates from the string.

Below is the sample code using java 8 streams.
package com.javaprogramto.programs.strings.remove.duplicates;

import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;
import java.util.stream.Collectors;

public class StringRemoveDuplicatesExample3 {

	public static void main(String[] args) {

		String orignalString = "world world";

		String output = Arrays.asList(orignalString.split(""))
								.stream()
								.distinct()
								.collect(Collectors.joining());

		System.out.println("Original String : " + orignalString);
		System.out.println("After removing the duplicates : " + output);

	}

}


In this solution part, first converted the string into a list using split("") method which does split each character as a single character string.

stream() method converts List<String> into Stream<String>.

Next, distinct() method deletes all duplicates strings from it. Here, distinct() method is a intermediate operation as result Stream<String> is returned.
Finally, collect() terminal operation that collects all string into a one string with the help of Collectors.joining() method.

5. Conclusion


In this article, We've seen how to delete all duplicates from the String using older jdk and new java 8 api.



No comments:

Post a Comment

Please do not add any spam links in the comments section.