Advertisement
ivana_andreevska

AV9 Names

Aug 16th, 2022
1,192
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.85 KB | None | 0 0
  1. package AV9;
  2.  
  3. import java.io.*;
  4. import java.util.HashMap;
  5. import java.util.HashSet;
  6. import java.util.Map;
  7. import java.util.Set;
  8. import java.util.stream.Collectors;
  9.  
  10. public class NamesTest {
  11.     public static void main(String[] args) {
  12.         try {
  13.             Map<String, Integer> boyNames = getNamesMap("C:\\Users\\User\\Desktop\\Napredno Sept\\src\\AV9\\boynames");
  14.             Map<String, Integer> girlNames = getNamesMap("C:\\Users\\User\\Desktop\\Napredno Sept\\src\\AV9\\girlnames");
  15.  
  16.             //find unisex names
  17.  
  18.             Set<String> uniqueNames = new HashSet<>();
  19.             uniqueNames.addAll(boyNames.keySet());
  20.             uniqueNames.addAll(girlNames.keySet());
  21.  
  22.             uniqueNames.stream()
  23.                     .filter(name -> boyNames.containsKey(name)
  24.                             && girlNames.containsKey(name))
  25.                     .forEach(System.out::println);
  26.  
  27.         } catch (FileNotFoundException e) {
  28.             e.printStackTrace();
  29.         }
  30.     }
  31.  
  32.     private static Map<String, Integer> getNamesMap(String path) throws FileNotFoundException {
  33.         InputStream inputStream = new FileInputStream(path);
  34.         Map<String, Integer> resultMap = new HashMap<>();
  35.         BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(path)));
  36.  
  37.         br.lines()
  38.                 .forEach(line -> {
  39.                     String[] parts = line.split("\\s+");
  40.                     String name = parts[0];
  41.                     int freq = Integer.parseInt(parts[1]);
  42.                     resultMap.put(name, freq);
  43.                 });
  44.  
  45. //        return br.lines()
  46. //                .collect(Collectors.toMap(
  47. //                        line -> line.split("\\s+")[0],
  48. //                        line -> Integer.parseInt(line.split("\\s")[1])
  49. //                ));
  50.         return resultMap;
  51.     }
  52. }
  53.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement