Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package AV9;
- import java.io.*;
- import java.util.HashMap;
- import java.util.HashSet;
- import java.util.Map;
- import java.util.Set;
- import java.util.stream.Collectors;
- public class NamesTest {
- public static void main(String[] args) {
- try {
- Map<String, Integer> boyNames = getNamesMap("C:\\Users\\User\\Desktop\\Napredno Sept\\src\\AV9\\boynames");
- Map<String, Integer> girlNames = getNamesMap("C:\\Users\\User\\Desktop\\Napredno Sept\\src\\AV9\\girlnames");
- //find unisex names
- Set<String> uniqueNames = new HashSet<>();
- uniqueNames.addAll(boyNames.keySet());
- uniqueNames.addAll(girlNames.keySet());
- uniqueNames.stream()
- .filter(name -> boyNames.containsKey(name)
- && girlNames.containsKey(name))
- .forEach(System.out::println);
- } catch (FileNotFoundException e) {
- e.printStackTrace();
- }
- }
- private static Map<String, Integer> getNamesMap(String path) throws FileNotFoundException {
- InputStream inputStream = new FileInputStream(path);
- Map<String, Integer> resultMap = new HashMap<>();
- BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(path)));
- br.lines()
- .forEach(line -> {
- String[] parts = line.split("\\s+");
- String name = parts[0];
- int freq = Integer.parseInt(parts[1]);
- resultMap.put(name, freq);
- });
- // return br.lines()
- // .collect(Collectors.toMap(
- // line -> line.split("\\s+")[0],
- // line -> Integer.parseInt(line.split("\\s")[1])
- // ));
- return resultMap;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement