Advertisement
javatechie

Find first non repeating character in a string in java 8

Feb 10th, 2023
824
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.75 KB | Source Code | 0 0
  1. public class Test {
  2.  
  3.     public static void main(String[] args) {
  4.         String input = "java spring boot java santosh";
  5.  
  6.         String key = Arrays.stream(input.split(""))
  7.                 .collect(Collectors.groupingBy(Function.identity(), LinkedHashMap::new, Collectors.counting())).
  8.                 entrySet().stream().filter(x -> x.getValue() == 1).findFirst().get().getKey();
  9.         System.out.println(key);
  10.  
  11.         LinkedHashMap<String, Long> map = Arrays.stream(input.split(""))
  12.                 .collect(Collectors.groupingBy(Function.identity(), LinkedHashMap::new, Collectors.counting()));
  13.         String result = map.entrySet().stream().filter(x -> x.getValue() == 1).findFirst().get().getKey();
  14.         System.out.println(result);
  15.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement