-1

I need method which will return person with the highest salary. I can't use void method.

private void persons() {
    persons = new ArrayList<>();
    persons.add(new Employee("Pa", "Ko", 2500));
    persons.add(new Employee("Ku", "No", 5000));
    persons.add(new Student("Ka", "Sl", 500));

I have tried something like this, but this doesn't work:

private Person bestPayed(List<Person> persons) {
    persons.stream().max(Comparator.comparingInt(Person::getSalary));
    return Person ;
Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
lookatdis
  • 1
  • 1
  • 1
    Possible duplicate of https://stackoverflow.com/questions/19338686/getting-max-value-from-an-arraylist-of-objects – Shubham Periwal Apr 25 '21 at 10:23
  • why they are different classes? your instance Employee and Student are different, it's better to use the same instance and define in a property in each class role of the person. – Pooya Chavoshi Apr 25 '21 at 10:23
  • Does this answer your question? [Getting max value from an arraylist of objects?](https://stackoverflow.com/questions/19338686/getting-max-value-from-an-arraylist-of-objects) – Shubham Periwal Apr 25 '21 at 10:25
  • You need to return `persons.stream().max(Comparator.comparingInt(Person::getSalary))` instead of `Person`. – geobreze Apr 25 '21 at 10:30
  • @geobreze your code is similar to mine but still it prints nothing but "Process finished with exit code 0" – lookatdis Apr 25 '21 at 10:40
  • To print something, try using `System.out.println(...)` – geobreze Apr 25 '21 at 10:42

1 Answers1

1

Your stream is almost ok because it returns Optional<Person>, then you need a get() to ensure return the Person object, not the optional.

Or, as Alex said into comments, use orElse to avoid exceptions.

So you can use this line into the method:

public Person bestPayed(List<Person> persons) {
  return persons.stream().max(Comparator.comparingInt(Person::getSalary)).get();
}

Or using orElse:

public Person bestPayed(List<Person> persons) {
  return persons.stream().max(Comparator.comparingInt(Person::getSalary)).orElse(null);
}

Is better using orElse() because if your list is empty, then Optional<Person> will be empty too and trying to get() it will throw an exception:

java.util.NoSuchElementException: No value present

Also your code fails (maybe is a typo) because you are returning Person, and, what is Person? You have to return the result of the stream.

And to output the object you can call the method like this:

System.out.println("BestPayed = "+bestPayed(persons));

And add a toString() method in your Person class. This method is to output the object as an string, and depends on what attributes your class have.

For example, assuming your class has name, surname and salary your toString() could be something like this:

@Override
public String toString() {
    return "Person [name=" + name + ", surname=" + surname + ", salary=" + salary + "]";
}

But you can add or delete what you want to output.

And running this code the console output is:

BestPayed = Person [name=Ku, surname=No, salary=5000]

Also if you only want to display the salary, use this:

System.out.println("Best salary = "+bestPayed(persons).getSalary());
J.F.
  • 13,927
  • 9
  • 27
  • 65
  • 1
    It's better to use `orElse` instead of `get()`, cause if list will be empty it will return `Optional.empty()` which will throw en exception on `get()` – Alex Apr 25 '21 at 11:01
  • That's true. I will add into the explanation. Also, as the list is added I've assumed there will exists at least one value. – J.F. Apr 25 '21 at 11:03
  • 1
    Btw i'd better return `Optional` from method and worked with it in functional style like `bestPayed(persons).ifPresent(System.out::println)` :) – Alex Apr 25 '21 at 11:07