Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR JAVA

Get Max Value Element From List With Objects

public static void main(String[] args) {
	
        Employee em = new Employee("Kiro",33,"Bulgaria"); //Creating objects
        Employee em2 = new Employee("Pesho",35,"Bulgaria");
        Employee em3 = new Employee("John",39,"UK");
        Employee em4 = new Employee("Mike",53,"USA");

        Cafe cafe = new Cafe("Malibu",4); //Creating Cafe & add the objects
        cafe.addEmployee(em);
        cafe.addEmployee(em2);
        cafe.addEmployee(em3);
        cafe.addEmployee(em4);

        System.out.println("The oldest is: " + cafe.getOldestEmployee()); //prints 53
    }
}

public int getOldestEmployee(){

        int oldest = 0;

        if (this.employees.size() > 1){

            Optional<Employee> max = this.employees
                    .stream()
                    .max(Comparator.comparingInt(Employee::getAge));

            oldest =  max.get().getAge();

        }else if (this.employees.size() == 1){

            oldest = this.employees.get(0).getAge();
        }
        return oldest;
    }
Source by stackoverflow.com #
 
PREVIOUS NEXT
Tagged: #Get #Max #Value #Element #From #List #With #Objects
ADD COMMENT
Topic
Name
5+6 =