Thursday, May 5, 2022

Question 16: Given a String, find the first non-repeated character in it using Stream functions?

import java.util.*; import*; import java.util.function.Function; public class JavaHungry { public static void main(String args[]) { String input = "Java Hungry Blog Alive is Awesome"; Character result = input.chars() // Stream of String .mapToObj(s -> Character.toLowerCase(Character.valueOf((char) s)))
                                    // First convert to Character object and then to lowercase .collect(Collectors.groupingBy(Function.identity(), LinkedHashMap::new, Collectors.counting()))
                                     //Store the chars in map with count .entrySet() .stream() .filter(entry -> entry.getValue() == 1L) .map(entry -> entry.getKey()) .findFirst() .get(); System.out.println(result); } }


Don't miss the next article! 
Be the first to be notified when a new article or Kubernetes experiment is published.                            

   Share This

You may also like

Kubernetes Microservices
Python AI/ML
Spring Framework Spring Boot
Core Java Java Coding Question
Maven AWS