I currently have a java Entities
: Cat
and Kitten
Below is the Cat entity that contains the current comparator
logic to order the Cat entities by which cat has the oldest Kitten
public class Cat implements Comparable<Cat>
{
//other fields and methods
@OneToMany(mappedBy = "cat",fetch = FetchType.EAGER)
private List<Kitten> kittenList= new ArrayList<Kitten>();
public DateTime getOldestBornValueForKittenInList(){
return kittenList.stream().min(Comparator.comparing(Kitten::getBorn))
.get().getBorn();
}
public int compareTo(Cat c) {
//fist compare on the oldest Created DateTime Value
int i = getOldestBornValueForKittenInList().compareTo(c.getOldestBornValueForKittenInList());
if (i != 0){
return i;
}
}
}
I no longer want my Cat Entity to implement the comparable
interface. Can I call the comparator in my application instead?
I.e. if I wanted an ordered Map
of cats and their list of kittens, with the cat with the oldest kitten being the first entry in the map:
Map<Cat, List<Kitten>> mapOfCatsAndKittens = new HashMap<Cat, List<Kitten>>();