Advertisement
Kancho

Persons_Age_2

May 14th, 2019
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.75 KB | None | 0 0
  1. package OpinionPoll;
  2.  
  3. import java.io.BufferedReader;
  4. import java.io.IOException;
  5. import java.io.InputStreamReader;
  6. import java.util.ArrayList;
  7. import java.util.List;
  8.  
  9. public class Main {
  10. public static void main(String[] args) throws IOException {
  11.  
  12. BufferedReader reader =
  13. new BufferedReader(
  14. new InputStreamReader(
  15. System.in));
  16.  
  17. System.out.println("Number of people ");
  18. int n = Integer.parseInt(reader.readLine());
  19. List<Persons> people = new ArrayList<>();
  20.  
  21. while (n -- > 0) {
  22. System.out.println("Person - age ");
  23. String []data = reader.readLine().split("\\s+");
  24.  
  25. Persons persons = new Persons(data[0], Integer.parseInt(data[1]));
  26. people.add(persons);
  27. }
  28.  
  29. /*people.stream().filter(persons -> persons.getAge() >= 20)
  30. .sorted(Comparator.comparingInt(Persons::getAge))
  31. .forEach(persons -> {
  32. System.out.println(persons.toString());
  33. });*/
  34.  
  35.  
  36.  
  37. people.stream().filter(persons -> persons.getAge() >= 20)
  38. .sorted((p2, p1) -> p1.getName().compareTo(p2.getName()))
  39. .forEach(persons -> {
  40. System.out.println(persons.toString());
  41. });
  42. }
  43. package OpinionPoll;
  44.  
  45. public class Persons {
  46.  
  47. private String name;
  48. private int age;
  49.  
  50. public Persons(String name, int age) {
  51. this.name = name;
  52. this.age = age;
  53. }
  54.  
  55. public String getName() {
  56. return this.name;
  57. }
  58.  
  59. public int getAge() {
  60. return this.age;
  61. }
  62.  
  63. @Override
  64. public String toString() {
  65. return String.format("%s - %d", name, age);
  66. }
  67. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement