0% found this document useful (0 votes)
14 views1 page

First Non-Repeated Character

Uploaded by

astra.payment
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views1 page

First Non-Repeated Character

Uploaded by

astra.payment
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 1

find the first non-repeated character from the below string using Java 8 Stream functions

String input = "Java Is Awesome"

The code provided indeed finds the first non-repeated character from the given string "Java Is
Awesome" using Java 8 Stream functions. Let's break down the code:

1. input.chars() converts the string into an IntStream of characters.

2. .mapToObj(x -> Character.toLowerCase(Character.valueOf((char) x))) maps each


character to its lowercase equivalent.

3. .collect(Collectors.groupingBy(Function.identity(), LinkedHashMap::new,
Collectors.counting())) groups the characters by their count using a LinkedHashMap to
maintain insertion order.

4. .entrySet().stream() converts the entry set of the map into a stream of entries.

5. .filter(entry -> entry.getValue() == 1) filters out the entries with count equal to 1.

6. .map(entry -> entry.getKey()) extracts the characters from the filtered entries.

7. .findFirst().get() gets the first character from the stream, which is the first non-repeated
character.

So yes, the provided code correctly finds the first non-repeated character from the given string.

Character output = input.chars()


.mapToObj(x -> Character.toLowerCase(Character.valueOf((char) x)))
.collect(Collectors.groupingBy(Function.identity(), LinkedHashMap::new,
Collectors.counting()))
.entrySet().stream()
.filter(entry -> entry.getValue() == 1)
.map(entry -> entry.getKey())
.findFirst().get();

You might also like