Advertisement
Guest User

Untitled

a guest
Aug 31st, 2015
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.85 KB | None | 0 0
  1. import java.io.*;
  2. import java.util.*;
  3. /*
  4. 1. Сооздай класс User(id, name, age)
  5. 2. Создай ArrayList из 5 разных пользователей(вынеси создание в отдельный метод)
  6. 3. Отсортируй лист по id и по name.
  7. */
  8.  
  9. public class Main {
  10.  
  11. public static class User {
  12.  
  13. private int id;
  14. private String name;
  15. private int age;
  16.  
  17. public User(int id, String name, int age) {
  18. this.name = name;
  19. this.age = age;
  20. this.id = id;
  21. }
  22.  
  23. public int compareTo(User u) {
  24. int result = this.name.compareTo(u.name);
  25. return result;
  26. }
  27. public String getName() {
  28. return this.name;
  29. }
  30. public int getAge() {
  31. return this.age;
  32. }
  33. public int getId() {
  34. return this.id;
  35. }
  36.  
  37. }
  38.  
  39. public static class byName implements Comparator<User> {
  40. public int compare(User u1, User u2) {
  41. return u1.getName().compareTo(u2.getName());
  42. }
  43. }
  44.  
  45. public static class byId implements Comparator<User> {
  46. public int compare(User u1, User u2) {
  47. return u1.getId() - u2.getId();
  48. }
  49. }
  50.  
  51. public static class byAge implements Comparator<User> {
  52. public int compare(User u1, User u2) {
  53. return u1.getAge() - u2.getAge();
  54. }
  55. }
  56.  
  57.  
  58.  
  59. public static void main(String[] args) throws Exception {
  60.  
  61. System.out.println("all rigth!");
  62. System.out.println("==========");
  63.  
  64. List<User> users = new ArrayList<User>();
  65. getArray(users);
  66.  
  67. System.out.println("by ID:");
  68. Collections.sort(users,new byId());
  69. for (User u : users) {
  70. System.out.println(u.getId() + " " + u.getName() + " " + u.getAge());
  71. }
  72. System.out.println("==========");
  73.  
  74. System.out.println("by name:");
  75. Collections.sort(users,new byName());
  76. for (User u : users) {
  77. System.out.println(u.getId() + " " + u.getName() + " " + u.getAge());
  78. }
  79. System.out.println("==========");
  80.  
  81. }
  82.  
  83.  
  84.  
  85.  
  86.  
  87. public static List getArray(List arlist) {
  88.  
  89. User ad1 = new User(1,"Masha",24);
  90. User ad2 = new User(2,"Dasha",25);
  91. User ad3 = new User(3,"Sasha",22);
  92. User ad4 = new User(4,"Natasha",21);
  93. User ad5 = new User(5,"Julia",27);
  94.  
  95. arlist.add(ad1);
  96. arlist.add(ad2);
  97. arlist.add(ad3);
  98. arlist.add(ad4);
  99. arlist.add(ad5);
  100.  
  101. return arlist;
  102. }
  103.  
  104. }
  105.  
  106.  
  107.  
  108.  
  109. /*
  110. all rigth!
  111. ==========
  112. by ID:
  113. 1 Masha 24
  114. 2 Dasha 25
  115. 3 Sasha 22
  116. 4 Natasha 21
  117. 5 Julia 27
  118. ==========
  119. by name:
  120. 2 Dasha 25
  121. 5 Julia 27
  122. 1 Masha 24
  123. 4 Natasha 21
  124. 3 Sasha 22
  125. ==========
  126.  
  127. Process finished with exit code 0
  128. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement