Day 28: RegEx, Patterns, and Intro to Databases

  • + 0 comments

    JAVA:

    import java.io.*;
    import java.math.*;
    import java.security.*;
    import java.text.*;
    import java.util.*;
    import java.util.concurrent.*;
    import java.util.function.*;
    import java.util.regex.*;
    import java.util.stream.*;
    import static java.util.stream.Collectors.joining;
    import static java.util.stream.Collectors.toList;
    
    
    
    public class Solution {
        public static void main(String[] args) throws IOException {
            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
    
            int N = Integer.parseInt(bufferedReader.readLine().trim());
    				
            HashMap<String, String> emails = new HashMap<>();
    
            IntStream.range(0, N).forEach(NItr -> {
                try {
                    String[] firstMultipleInput = bufferedReader.readLine().replaceAll("\\s+$", "").split(" ");
    
                    String firstName = firstMultipleInput[0];
    
                    String emailID = firstMultipleInput[1];
                    
                    emails.put(emailID, firstName);
    
                } catch (IOException ex) {
                    throw new RuntimeException(ex);
                }
            });
    
            //Stream emails, filter those that contains @gmail.com, collect to  list, sort list, print list.
            List<String> sortedNames = emails.entrySet().stream().filter(entry -> entry.getKey().contains("@gmail.com"))
                .map(entry -> entry.getValue()).sorted().collect(Collectors.toList());
            
            sortedNames.forEach(System.out::println);
        }
    }