deyanmalinov

03. First Name - Optional

Apr 21st, 2020
393
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package DPM;
  2.  
  3. import java.util.*;
  4. import java.util.stream.Stream;
  5.  
  6. public class Main {
  7.     public static void main(String[] args) {
  8.         Scanner scan = new Scanner(System.in);
  9.         List<String> names = Arrays.asList(scan.nextLine().split(" "));
  10.         HashSet<Character> letters = new HashSet<>();
  11.  
  12.         Stream.of(scan.nextLine()
  13.                 .split(" "))
  14.                 .map(ch -> ch.toLowerCase().charAt(0))
  15.                 .forEach(ch -> letters.add(ch));
  16.         Optional<String> name = names.stream()
  17.                 .filter(str -> letters.contains(str.toLowerCase().charAt(0)))
  18.                 .sorted()
  19.                 .findFirst();
  20.         if (name.isPresent()){
  21.             System.out.println(name.get());
  22.         }else {
  23.             System.out.println("No match");
  24.         }
  25.     }
  26. }
RAW Paste Data