0% found this document useful (0 votes)
13 views2 pages

First Non-Repeated Character in Java 8

Java interview question
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)
13 views2 pages

First Non-Repeated Character in Java 8

Java interview question
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

Java Program To Find First

Non-Repeated Character In
String
First we will split string based on “” empty string value
and will store into Array of string and after that we will
use [Link](-) method and convert into stream
and after converting into stream we can
call .collect(-) [Link] collect(-) method we can
pass the parameter [Link](-,-) and
Inside [Link](-,-) function we can pass
the two parameters one is [Link]() to find the
unique identity of each element and second parameter we
can paas [Link]() to count the number of
occurrence for each character. After that we again need to
convert Map into stream to apply stream functions. So we
can call .entrySet().stream() and now we can apply the
filter and restrict the data based on condition .filter(ele -
> [Link]() == 1) . Now we will store all the non
repeated keys into ArrayList and will pick the first non
repeated character and Print the output.

Below is the complete Code :

package [Link];

import [Link];
import [Link];
import [Link];
import [Link];

public class FindFirstNonRepeatedCharacter {

public static void main(String[] args) {

String input = "gainjavaknowledge";


String output = [Link]([Link](""))
.collect([Link]([Link](),
LinkedHashMap::new, [Link]()))
.entrySet().stream().filter(e -> [Link]() == 1)
.map(e -> [Link]()).findFirst().get();
[Link]("First Non Repeated Character : " + output);
}
}

You might also like