fork download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.lang.*;
  5. import java.io.*;
  6.  
  7. /* Name of the class has to be "Main" only if the class is public. */
  8. class Ideone
  9. {
  10. private static int children = 7;
  11.  
  12. public static void main (String[] args) throws java.lang.Exception
  13. {
  14. List<School> list = new ArrayList<>( );
  15. list.add(new School(0,20,0,"Ugly"));
  16. list.add(new School(30,20,50,"Normal"));
  17. list.add(new School(50,20,100,"Best"));
  18.  
  19. Optional<School> max = list.stream().max( (el1, el2) -> el1.getEducation() - el2.getEducation() );
  20. if (max.isPresent()) {
  21. School school = max.get();
  22. school.setSpace(school.getSpace() - children);
  23. }
  24.  
  25.  
  26. School maxSchool = null;
  27. for (School school : list) {
  28. if (maxSchool == null) {
  29. maxSchool = school;
  30. } else if (maxSchool.getEducation() < school.getEducation()) {
  31. maxSchool = school;
  32. }
  33. }
  34. if (maxSchool != null) {
  35. maxSchool.setSpace(maxSchool.getSpace() - children);
  36. }
  37.  
  38. maxSchool = Collections.max( list, new Comparator<School>() {
  39. @Override
  40. public int compare(School o1, School o2) {
  41. return o1.getEducation() - o2.getEducation();
  42. }
  43. } );
  44. if (maxSchool != null) {
  45. maxSchool.setSpace(maxSchool.getSpace() - children);
  46. }
  47. System.out.println( list );
  48. }
  49. }
  50.  
  51.  
  52. class School {
  53.  
  54. private int education = 0;
  55. private int space = 20;
  56. private int cost = 0;
  57. private String name = " ";
  58.  
  59. public School(int education, int space, int cost, String name) {
  60. this.cost = cost;
  61. this.space = space;
  62. this.education = education;
  63. this.name = name;
  64. }
  65.  
  66. public int getEducation() {
  67. return education;
  68. }
  69.  
  70. public void setEducation(int education) {
  71. this.education = education;
  72. }
  73.  
  74. public int getSpace() {
  75. return space;
  76. }
  77.  
  78. public void setSpace(int space) {
  79. this.space = space;
  80. }
  81.  
  82. public int getCost() {
  83. return cost;
  84. }
  85.  
  86. public void setCost(int cost) {
  87. this.cost = cost;
  88. }
  89.  
  90. public String getName() {
  91. return name;
  92. }
  93.  
  94. public void setName(String name) {
  95. this.name = name;
  96. }
  97.  
  98. @Override
  99. public String toString() {
  100. return "School{" +
  101. "education=" + education +
  102. ", space=" + space +
  103. ", cost=" + cost +
  104. ", name='" + name + '\'' +
  105. '}';
  106. }
  107. }
Success #stdin #stdout 0.21s 320832KB
stdin
Standard input is empty
stdout
[School{education=0, space=20, cost=0, name='Ugly'}, School{education=30, space=20, cost=50, name='Normal'}, School{education=50, space=-1, cost=100, name='Best'}]