Advertisement
Guest User

Untitled

a guest
Apr 19th, 2019
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.05 KB | None | 0 0
  1. public class TEsr {
  2. List<Person> set = new ArrayList();
  3. public static void main(String[] args) throws IOException {
  4. TEsr tesr = new TEsr();
  5. tesr.go();
  6.  
  7.  
  8. }
  9. public void go() throws IOException {
  10. set.add(new Person("Maks",42,true));
  11. set.add(new Person("Andy",22,false));
  12. set.add(new Person("Lex",99,false));
  13. set.add(new Person("Mary",18,false));
  14.  
  15. if(set.isEmpty()) {
  16. throw new IOException("collection is empty");
  17. }
  18. else {
  19. Collections.sort(set, new Sort());
  20. set.remove(3);
  21. set.remove(2);
  22. set.remove(1);
  23. System.out.println(set);
  24. }
  25. }
  26. }
  27. class Person {
  28. String name;
  29. int age;
  30. boolean men;
  31. Person(String name,int age,boolean men) {
  32. this.age = age;
  33. this.name = name;
  34. this.men = men;
  35. }
  36. public String toString()
  37. {
  38. return this.age + " " + this.name +
  39. " " + this.men;
  40. }
  41. }
  42. class Sort implements Comparator<Person> {
  43. public int compare(Person a, Person b)
  44. {
  45. return b.age - a.age;
  46. }
  47. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement