In  this tutorial, we will see “How to find the first non-repeated character in a string using Java 8?”find first non repeated character in a string using Java 8

Java 8 – How to find the First Non-Repeated Character in a String? Click To Tweet

 

Output:

find first non repeated character in a string using Java 8

In the above example, The firstNonRepeatedCharacter method takes a String input which is supplied through the main method and returns an Optional<Character> which contains the first non-repeated character in the supplied string, or an empty Optional if there are no non-repeated characters present there. It uses stream API and collects the frequency of each character in the input string into a Map of Character to Long. The filtered stream of characters is later used to find the first character whose frequency is 1.

Java 8 – How to find the First Non-Repeated Character in a String? Click To Tweet

Do you like this Post? – then check my other helpful posts:

Other Useful References: