Use Java Streams to sort the Specialty objects by their name

Signed-off-by: Antoine Rey <antoine.rey@free.fr>
This commit is contained in:
Antoine Rey 2025-01-19 19:13:56 +01:00 committed by Dave Syer
parent b46b97a3e7
commit 73d73609b5

View file

@ -15,14 +15,13 @@
*/
package org.springframework.samples.petclinic.vet;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
import org.springframework.beans.support.MutableSortDefinition;
import org.springframework.beans.support.PropertyComparator;
import org.springframework.samples.petclinic.model.NamedEntity;
import org.springframework.samples.petclinic.model.Person;
import jakarta.persistence.Entity;
@ -59,9 +58,9 @@ public class Vet extends Person {
@XmlElement
public List<Specialty> getSpecialties() {
List<Specialty> sortedSpecs = new ArrayList<>(getSpecialtiesInternal());
PropertyComparator.sort(sortedSpecs, new MutableSortDefinition("name", true, true));
return Collections.unmodifiableList(sortedSpecs);
return getSpecialtiesInternal().stream()
.sorted(Comparator.comparing(NamedEntity::getName))
.collect(Collectors.toList());
}
public int getNrOfSpecialties() {