如何按枚举顺序排序java



你好,我正在尝试按以下顺序对列表进行排序

Breakfast -> Lunch -> Dinner -> Dessert->Breakfast -> Lunch -> Dinner -> Dessert

getAllMealPlans([蛋糕(甜点(,蔬菜汤(午餐(,华夫饼(早餐(,土豆烤面包(晚餐(],4(

返回包含四个计划的集合:

* - [ Waffles (breakfast), Vegetable Soup (lunch), Potato Gratin (dinner), Cake (dessert)]
* - [Vegetable Soup (lunch), Potato Gratin (dinner), Cake (dessert), Waffles (breakfast)]
* - [Potato Gratin (dinner), Cake (dessert), Waffles (breakfast), Vegetable Soup (lunch)]
* - [Cake (dessert), Waffles (breakfast), Vegetable Soup (lunch), Potato Gratin (dinner)]

这就是枚举和食物类型的

public enum Meal {
BREAKFAST, LUNCH, DINNER, DESSERT
}
public static class Food {
String dish;
public Meal meal;
public Food(String dish, Meal meal) {
this.dish = dish;
this.meal = meal;
}

这就是我所拥有的——我无法完全完成分拣工作。我有什么想法吗?

public static Set<List<Food>> getAllMealPlans(Set<Food> foods, int numMeals) {
Set<List<Food>> set = new HashSet<>();
List<Food> aList = new ArrayList<Food>(foods);
List<String> sortedList = aList.stream().map(Meal::valueOf).sorted(Meal::compareTo).map(Meal::toString).collect(Collectors.toList());
System.out.println(sortedList);
return set;
}

我认为您想要做的是对列表进行排序"a列表";吃饭的时候。。。

这应该工作

aList.stream().sorted((a,b) -> a.meal.compareTo(b.meal)).collect(Collectors.toList());

您可以通过使用";flatMap";像这个

List<Food> listOfFoods = mealPlans.stream().flatMap(List<Food>::stream).collect(Collectors.toList());
System.out.println(Arrays.toString(listOfFoods.toArray()));

您想要在Food类中实现Comparable(或者像Javaman的回复中那样编写一个Comparator(。参见例如。https://howtodoinjava.com/java/collections/java-comparable-interface/.

我还为Food编写了一个toString方法,这样你就不必映射到String来以一种漂亮的方式显示它。

但是,也许您使用了错误的数据类型?你不想在这里有一张地图吗?这样你就可以查早餐吃什么了?关键是枚举和Food的值。SortedMap应该按照正确的顺序为您提供项目。

实际问题是什么?你的描述和代码不太匹配。你想要所有可能的计划,即菜和饭的组合,还是只想要一个排序的计划?

呈现的代码片段有几个问题:

  1. numMeals参数未在getAllMealPlans中的任何位置使用
  2. 始终返回空结果set——未填充
  3. 编译错误:aListFood的列表,它从未映射到Meal以应用Meal的方法引用
  4. List<Food> aList = new ArrayList<Food>(foods);的冗余包装

由于预期的输出是提供一组包含按一个移位的食物列表的集合,这可以按照如下所示的方式实现,同时修复上述问题:

public static Set<List<Food>> getAllMealPlans(Set<Food> foods, int numMeals) {
Set<List<Food>> set = new LinkedHashSet<>(); // LinkedHashSet to keep insertion order
// sort food by meal
List<Food> sortedList = foods.stream()
.sorted(Comparator.comparing(Food::getMeal)) // or Comparator.comparing(food -> food.meal)
.collect(Collectors.toList());
// populate the result set starting from sorted list
for (int i = 0; i < numMeals; i++) {

set.add(sortedList);
// rotated shift left by 1
Food first = sortedList.get(0);
sortedList = new ArrayList<>(sortedList.subList(1, sortedList.size()));
sortedList.add(first);
}
return set;
}

方法Food::toString需要重写如下:

static class Food {
//...
@Override
public String toString() {
return String.format("%s (%s)", dish, meal.name().toLowerCase()); 
}
}

测试

getAllMealPlans(Set.of(
new Food("Cake", Meal.DESSERT), 
new Food("Vegetable Soup", Meal.LUNCH), 
new Food("Waffles", Meal.BREAKFAST), 
new Food("Potato Gratin", Meal.DINNER)
), 4).forEach(System.out::println);

输出:

[Waffles (breakfast), Vegetable Soup (lunch), Potato Gratin (dinner), Cake (dessert)]
[Vegetable Soup (lunch), Potato Gratin (dinner), Cake (dessert), Waffles (breakfast)]
[Potato Gratin (dinner), Cake (dessert), Waffles (breakfast), Vegetable Soup (lunch)]
[Cake (dessert), Waffles (breakfast), Vegetable Soup (lunch), Potato Gratin (dinner)]

最新更新