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