This following program illustrate 4 different ways to find solution to the above problem. we will get back to find solution using lambda expressions through fourth function.
Remember to set compiler path as java 8.
NonRepeated.find("abhishek") will get it through indexOf and lastindexOf build in functions.
NonRepeated.findsec("learnNShare") NonRepeated.findsec("learnNShare") are same only difference is we are using HashMap and LinkedHashMap respectively which in our case is not making any difference.
Finally findfourth("letseethistimel", System.out::print); makes use of lambda expressions to arrive at solutions without making use of second loop or build in functions.
For now the complexity of each function is left to user to find. I will work on same and update this post in near future.
/**
*
*/
package com.abhishek.string;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.function.*;
import java.util.stream.Collectors;
import static java.util.function.Function.identity;
/**
* @author Abhishek
*
*/
/*
* Java program to find First Non Repeated Occurrence using lambda expressions
*
package com.abhishek.string;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.function.*;
import java.util.stream.Collectors;
import static java.util.function.Function.identity;
public class NonRepeated {
public NonRepeated() {
}
public static void main(String[] args) {
try {
//Hard coding input to illustrate various methods
char ch = NonRepeated.find("abhishek");
Character ch1 = NonRepeated.findsec("learnNShare");
Character ch2 = NonRepeated.findthird("learnNShare");
System.out.print(ch+" "+ch1+" "+ch2+" ");
//using java 8 feature lambda expression
findfourth("letseethistimel", System.out::print);
} catch (Exception e) {
e.printStackTrace();
}
}
public static char find(String pstr){
for( char ch: pstr.toCharArray()){
if(pstr.indexOf(ch)==pstr.lastIndexOf(ch)){
return ch;
}
}
return '/';
}
public static Character findsec(String pstr){
int len = pstr.length();
Character ch ;
HashMap<Character,Integer> chmap = new HashMap<Character,Integer>();
for(int i=0;i<len;i++){
ch = pstr.charAt(i);
if(chmap.containsKey(ch))
chmap.put(ch, chmap.get(ch)+1);
else
chmap.put(ch, 1);
}
for(int i=0;i<len;i++){
ch = pstr.charAt(i);
if(chmap.get(ch)==1)
return ch;
}
return null;
}
public static Character findthird(String pstr){
int len = pstr.length();
Character ch = 'a';
LinkedHashMap<Character,Integer> chmap = new LinkedHashMap<Character,Integer>();
for(int i=0;i<len;i++){
ch = pstr.charAt(i);
if(chmap.containsKey(ch))
chmap.put(ch, chmap.get(ch)+1);
else
chmap.put(ch, 1);
}
for(int i=0;i<len;i++){
ch = pstr.charAt(i);
if(chmap.get(ch)==1)
return ch;
}
return ch;
}
public static void findfourth(String pstr, Consumer<Character> callback) {
pstr.chars()
.mapToObj(in -> Character.valueOf((char) in))
.collect(Collectors.groupingBy(identity(), LinkedHashMap::new, Collectors.counting()))
.entrySet().stream()
.filter(entry -> entry.getValue() == 1L)
.map(entry -> entry.getKey())
.findFirst().map(cnt -> { callback.accept(cnt); return cnt; } );
}
}
Output will be
a l l h
Remember to set compiler path as java 8.
NonRepeated.find("abhishek") will get it through indexOf and lastindexOf build in functions.
NonRepeated.findsec("learnNShare") NonRepeated.findsec("learnNShare") are same only difference is we are using HashMap and LinkedHashMap respectively which in our case is not making any difference.
Finally findfourth("letseethistimel", System.out::print); makes use of lambda expressions to arrive at solutions without making use of second loop or build in functions.
For now the complexity of each function is left to user to find. I will work on same and update this post in near future.
/**
*
*/
package com.abhishek.string;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.function.*;
import java.util.stream.Collectors;
import static java.util.function.Function.identity;
/**
* @author Abhishek
*
*/
/*
* Java program to find First Non Repeated Occurrence using lambda expressions
*
package com.abhishek.string;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.function.*;
import java.util.stream.Collectors;
import static java.util.function.Function.identity;
public class NonRepeated {
public NonRepeated() {
}
public static void main(String[] args) {
try {
//Hard coding input to illustrate various methods
char ch = NonRepeated.find("abhishek");
Character ch1 = NonRepeated.findsec("learnNShare");
Character ch2 = NonRepeated.findthird("learnNShare");
System.out.print(ch+" "+ch1+" "+ch2+" ");
//using java 8 feature lambda expression
findfourth("letseethistimel", System.out::print);
} catch (Exception e) {
e.printStackTrace();
}
}
public static char find(String pstr){
for( char ch: pstr.toCharArray()){
if(pstr.indexOf(ch)==pstr.lastIndexOf(ch)){
return ch;
}
}
return '/';
}
public static Character findsec(String pstr){
int len = pstr.length();
Character ch ;
HashMap<Character,Integer> chmap = new HashMap<Character,Integer>();
for(int i=0;i<len;i++){
ch = pstr.charAt(i);
if(chmap.containsKey(ch))
chmap.put(ch, chmap.get(ch)+1);
else
chmap.put(ch, 1);
}
for(int i=0;i<len;i++){
ch = pstr.charAt(i);
if(chmap.get(ch)==1)
return ch;
}
return null;
}
public static Character findthird(String pstr){
int len = pstr.length();
Character ch = 'a';
LinkedHashMap<Character,Integer> chmap = new LinkedHashMap<Character,Integer>();
for(int i=0;i<len;i++){
ch = pstr.charAt(i);
if(chmap.containsKey(ch))
chmap.put(ch, chmap.get(ch)+1);
else
chmap.put(ch, 1);
}
for(int i=0;i<len;i++){
ch = pstr.charAt(i);
if(chmap.get(ch)==1)
return ch;
}
return ch;
}
public static void findfourth(String pstr, Consumer<Character> callback) {
pstr.chars()
.mapToObj(in -> Character.valueOf((char) in))
.collect(Collectors.groupingBy(identity(), LinkedHashMap::new, Collectors.counting()))
.entrySet().stream()
.filter(entry -> entry.getValue() == 1L)
.map(entry -> entry.getKey())
.findFirst().map(cnt -> { callback.accept(cnt); return cnt; } );
}
}
Output will be
a l l h
No comments:
Post a Comment