嵌套列表Java中基于日期字段的主列表排序
我有下面的对象列表,需要根据日期前的成分对其进行排序。因此,最古老的、最好的、最早的食谱应该排在最后嵌套列表Java中基于日期字段的主列表排序,java,sorting,java-stream,Java,Sorting,Java Stream,我有下面的对象列表,需要根据日期前的成分对其进行排序。因此,最古老的、最好的、最早的食谱应该排在最后 [ { "id": 2, "recipeName": "Burger", "createdDate": "2020-11-22T00:00:00.000+00:00", "ingredients": [
[
{
"id": 2,
"recipeName": "Burger",
"createdDate": "2020-11-22T00:00:00.000+00:00",
"ingredients": [
{
"ingredientId": 3,
"ingredientName": "Burger Bun",
"category": "",
"useBy": "2020-12-20",
"bestBefore": "2020-12-23"
},
{
"ingredientId": 4,
"ingredientName": "Beef Pattie",
"category": "",
"useBy": "2020-12-20",
"bestBefore": "2020-12-23"
},
{
"ingredientId": 5,
"ingredientName": "Tomato",
"category": "",
"useBy": "2020-12-20",
"bestBefore": "2020-09-10"
}
]
},
{
"id": 3,
"recipeName": "Chicken Salad",
"createdDate": "2020-11-22T00:00:00.000+00:00",
"ingredients": [
{
"ingredientId": 7,
"ingredientName": "Chicken",
"category": "",
"useBy": "2020-12-20",
"bestBefore": "2020-12-23"
},
{
"ingredientId": 6,
"ingredientName": "Salad Mix",
"category": "",
"useBy": "2020-12-20",
"bestBefore": "2020-11-15"
}
]
}
]
我已经尝试了下面的代码,但它只是对配方中的成分进行排序,而不是列表中的配方对象。这是否适用于Java8流
List<Recipes> finalList = filteredList.stream().sorted((o1, o2) -> (int) (o1.getId() - o2.getId()))
.map(recipes -> {
List<Ingredients> in = recipes.getIngredients().stream()
.sorted(Comparator.comparing(Ingredients::getBestBefore).reversed()).collect(Collectors.toList());
recipes.setIngredients(in);
return recipes;
}).collect(Collectors.toList());
List finalList=filteredList.stream().sorted((o1,o2)->(int)(o1.getId()-o2.getId())
.map(食谱->{
列表in=recipes.getComponents().stream()
.sorted(Comparator.comparing(配料::getBestBefore).reversed()).collect(collector.toList());
配方。成分(单位:英寸);
返回食谱;
}).collect(Collectors.toList());
因为您需要确保,如果任何配料在日期之前具有最古老的最佳配方,则该配方应排在列表的最后一位。您需要确定配方成分中的max
(最早)日期,您可以为此创建如下方法:
private static Date findOldestBestBeforeForRecipe(Recipes recipes) {
return recipes.getIngredients()
.stream()
.map(Ingredients::getBestBefore)
.max(Comparator.naturalOrder())
.orElseThrow(() -> new IllegalArgumentException("recipes without ingredients!!"));
}
然后对你的食谱进行排序,这样会简化,比如为这个标准定义一个比较器
,并将它们附加到现有的比较中
Comparator<Recipes> ingredientsBestBeforeDate = (o1, o2) ->
findOldestBestBeforeForRecipe(o2).compareTo(findOldestBestBeforeForRecipe(o1));
List<Recipes> finalList = filteredList.stream()
.sorted(Comparator.comparingInt(Recipes::getId)
.thenComparing(ingredientsBestBeforeDate))
.collect(Collectors.toList());
比较器IngCreditsBestForeDate=(o1,o2)->
findOldestBestBeforeForRecipe(o2)。与(findOldestBestBeforeForRecipe(o1))相比;
List finalList=filteredList.stream()
.sorted(Comparator.comparingInt(Recipes::getId)
.然后比较(在日期之前)
.collect(Collectors.toList());
因为您需要确保,如果任何配料在日期之前具有最古老的最佳配方,则该配方应排在列表的最后一位。您需要确定配方成分中的max
(最早)日期,您可以为此创建如下方法:
private static Date findOldestBestBeforeForRecipe(Recipes recipes) {
return recipes.getIngredients()
.stream()
.map(Ingredients::getBestBefore)
.max(Comparator.naturalOrder())
.orElseThrow(() -> new IllegalArgumentException("recipes without ingredients!!"));
}
然后对你的食谱进行排序,这样会简化,比如为这个标准定义一个比较器
,并将它们附加到现有的比较中
Comparator<Recipes> ingredientsBestBeforeDate = (o1, o2) ->
findOldestBestBeforeForRecipe(o2).compareTo(findOldestBestBeforeForRecipe(o1));
List<Recipes> finalList = filteredList.stream()
.sorted(Comparator.comparingInt(Recipes::getId)
.thenComparing(ingredientsBestBeforeDate))
.collect(Collectors.toList());
比较器IngCreditsBestForeDate=(o1,o2)->
findOldestBestBeforeForRecipe(o2)。与(findOldestBestBeforeForRecipe(o1))相比;
List finalList=filteredList.stream()
.sorted(Comparator.comparingInt(Recipes::getId)
.然后比较(在日期之前)
.collect(Collectors.toList());
当您说根据日期前最好的配料进行排序时,要对整个配方进行排序,应考虑哪种配料?(将具有最差和最佳值的bestBefore
成分作为同一配方的一部分,该配方在您的排序列表中的位置是什么?@Naman如果任何成分具有最早的bestBefore
该配方应排在列表的最后一位。在这种情况下,汉堡包对象应该排在列表的最后,因为西红柿是最古老的bestBeforedate
,当您说基于日期前最佳配料进行排序时,应考虑对整个配方进行排序的配料是什么?(将具有最差和最佳值的bestBefore
成分作为同一配方的一部分,该配方在您的排序列表中的位置是什么?@Naman如果任何成分具有最早的bestBefore
该配方应排在列表的最后一位。在这种情况下,Burger对象应该排在列表的最后,因为西红柿具有最古老的bestBeforedate
这不是我所期望的结果,任何包含旧bestbefore成分的配方(配方应根据bestbefore排序)编辑答案后,日期应放在列表的底部,这样才能获得结果List finalList=filteredList.stream().sorted(ingredientsbestforedate.collector.toList())代码>和下面的修复在findOldestBestBeforeForRecipe
.min(Comparator.naturalOrder())
这不是我所期望的结果,任何包含旧bestbefore成分的配方(配方应根据bestbefore排序)编辑答案后,日期应放在列表的底部,这样才能获得结果List finalList=filteredList.stream().sorted(ingredientsbestforedate.collector.toList())代码>和下面的修复在findOldestBestBeforeForRecipe
.min(Comparator.naturalOrder())