I have a Spring boot project wich store data in database.
I want to get it by sorting dby date desc.
This is example of code :
//Furit family entity store in database
@Entity(name = “FruitFamily”)
public class FruitFamilyEntity {
@Column(name = "user_id", nullable = false)
private String userId;
@Column(name = "fruitfamily_id", updatable = false, nullable = false)
private UUID fruitFamilyId;
@Enumerated(EnumType.STRING)
@Column(name = "typeFruit", updatable = false, nullable = false)
private TypeFruit typeFruit;
@Column(name = "purchase_date")
private LocalDateTime purchaseDate;
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || Hibernate.getClass(this) != Hibernate.getClass(o)) return false;
LifestyleEntity that = (LifestyleEntity) o;
return getId() != null && Objects.equals(getId(), that.getId());
}
@Override
public int hashCode() {
return getClass().hashCode();
}
}
@Getter
@AllArgsConstructor
public enum TypeFruit {
STONE_FRUIT("Fruit à noyau"),
POME_FRUIT("Fruit à pepin"));
private final String display;
}
@Getter
@Setter()
@NoArgsConstructor
@AllArgsConstructor
public class FruitFamily {
private String userId;
private UUID fruitFamilyId;
private TypeFruit typeFruit;
private LocalDateTime purchaseDate;
}
public class FruitStore {
public Map<TypeFruit, List<FruitFamily>> allFruitFamily() {
FruitFamily fruit1 = new FruitFamily("1","8244f3ca-1881-44e3",STONE_FRUIT,LocalDateTime.parse("2024-05-02T16:15:54.332"))
FruitFamily fruit2 = new FruitFamily("2","8244f3ca-1880-45e3",STONE_FRUIT,LocalDateTime.parse("2024-05-01T18:34:28.127"))
FruitFamily fruit3 = new FruitFamily("3","8244f3ca-1882-46e3",STONE_FRUIT,LocalDateTime.parse("2024-01-28T10:04:28.187"))
FruitFamily fruit4 = new FruitFamily("4","8244f3ca-1883-47e3",STONE_FRUIT,LocalDateTime.parse("2023-07-18T22:20:45.332"))
FruitFamily fruit5 = new FruitFamily("5","8244f3ca-1879-48e3",POME_FRUIT,LocalDateTime.parse("2023-05-01T22:20:45.332"))
FruitFamily fruit6 = new FruitFamily("6","8244f3ca-18767-49e3",POME_FRUIT,LocalDateTime.parse("2023-05-02T12:35:26.443"))
FruitFamily fruit7 = new FruitFamily("7","8244f3ca-1841-42e3",POME_FRUIT,LocalDateTime.parse("2023-09-13T14:50:26.332"))
FruitFamily fruit8 = new FruitFamily("8","8244f3ca-1823-41e3",POME_FRUIT,LocalDateTime.parse("2024-05-02T15:23:45.332"))
FruitFamily arrFruit[] = {fruit1,fruit2,fruit3,fruit4,fruit5,fruit6,fruit7,fruit8};
var listFruitFamily = Arrays.asList(arrFruit);
EnumMap<TypeFruit, List<FruitFamily>> result = new EnumMap<>(TypeFruit.class);
listFruitFamily.forEach(fruitFam -> {
var listByTypeFruit = result.computeIfAbsent(fruitFam.getTypeFruit(), k -> new ArrayList<>());
listByTypeFruit.add(fruitFam);
});
return result;
}
public static void main(String[] args)
{
}
}
I want to filter my result by most recent purchase date by type of fruit
[STONE_FRUIT :
[
{"userId":"1","fruitFamilyId":"8244f3ca-1881-44e3","userId":STONE_FRUIT,"purchaseDate":"2024-05-02T16:15:54.332"},
{"userId":"2","fruitFamilyId":"8244f3ca-1881-44e3","userId":STONE_FRUIT,"purchaseDate":"2024-05-01T18:34:28.127"},
{"userId":"3","fruitFamilyId":"8244f3ca-1881-44e3","userId":STONE_FRUIT,"purchaseDate":"2024-01-28T10:04:28.187"},
{"userId":"4","fruitFamilyId":"8244f3ca-1881-44e3","userId":STONE_FRUIT,"purchaseDate":"2023-07-18T22:20:45.332"}
]
],
[POME_FRUIT :
[
{"userId":"8","fruitFamilyId":"8244f3ca-1881-44e3","userId":POME_FRUIT,"purchaseDate":"2024-05-02T15:23:45.332"},
{"userId":"7","fruitFamilyId":"8244f3ca-1881-44e3","userId":POME_FRUIT,"purchaseDate":"2023-09-13T14:50:26.3323"},
{"userId":"6","fruitFamilyId":"8244f3ca-1881-44e3","userId":POME_FRUIT,"purchaseDate":"2023-05-02T12:35:26.443"},
{"userId":"5","fruitFamilyId":"8244f3ca-1881-44e3","userId":POME_FRUIT,"purchaseDate":"2023-05-01T22:20:45.332"}
]
]
My allFruitFamily function does not filter by purchase date desc. how to get the result above