SHARE
TWEET

Students 2.0 Map Solution

a guest Mar 28th, 2019 243 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package students2;
  2.  
  3.  
  4. import java.util.*;
  5.  
  6. public class Main {
  7.     public static void main(String[] args) {
  8.         Scanner scanner = new Scanner(System.in);
  9.  
  10.         Map<String, Student> namesStudent = new LinkedHashMap<>();
  11.         String data = scanner.nextLine();
  12.  
  13.         while (!data.equals("end")) {
  14.             String[] tokens = data.split("\\s+");
  15.             String firstName = tokens[0];
  16.             String lastName = tokens[1];
  17.             int age = Integer.parseInt(tokens[2]);
  18.             String city = tokens[3];
  19.  
  20.             String names = firstName + " " + lastName;
  21.             if (!namesStudent.containsKey(names)) {
  22.                 Student student = new Student(firstName, lastName, age, city);
  23.                 namesStudent.put(names, student);
  24.             } else {
  25.                 namesStudent.get(names).setAge(age);
  26.                 namesStudent.get(names).setCity(city);
  27.             }
  28.  
  29.             data = scanner.nextLine();
  30.         }
  31.  
  32.         String filterCity = scanner.nextLine();
  33.         namesStudent.entrySet().stream()
  34.                 .filter(kvp -> kvp.getValue().getCity().equals(filterCity))
  35.                 .forEach(kvp -> System.out.println(String.format("%s %s is %d years old", kvp.getValue().firstName, kvp.getValue().lastName, kvp.getValue().age)));
  36.     }
  37.  
  38.     static class Student {
  39.         private String firstName;
  40.         private String lastName;
  41.         private int age;
  42.         private String city;
  43.  
  44.         public Student(String firstName, String lastName, int age, String city) {
  45.             this.firstName = firstName;
  46.             this.lastName = lastName;
  47.             this.age = age;
  48.             this.city = city;
  49.         }
  50.  
  51.         public String getFirstName() {
  52.             return firstName;
  53.         }
  54.  
  55.         public void setFirstName(String firstName) {
  56.             this.firstName = firstName;
  57.         }
  58.  
  59.         public String getLastName() {
  60.             return lastName;
  61.         }
  62.  
  63.         public void setLastName(String lastName) {
  64.             this.lastName = lastName;
  65.         }
  66.  
  67.         public int getAge() {
  68.             return age;
  69.         }
  70.  
  71.         public void setAge(int age) {
  72.             this.age = age;
  73.         }
  74.  
  75.         public String getCity() {
  76.             return city;
  77.         }
  78.  
  79.         public void setCity(String city) {
  80.             this.city = city;
  81.         }
  82.     }
  83. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top